/    Sign up×
Community /Pin to ProfileBookmark

Validation for "Next" button using DataTables

Hello everybody!
I’m using datatables to show results from a survey. I show each question and a set of answers to choose from in a separate page. I have to do validation – if you have not chosen any of the possible answers, to stay at that page and show validation massage for required field. If you answer that question, then to be shown the next question. That’s why I need to validate form for the “Next” button. Not to show next question, if it’s not chosed answer.
Now I saw id of the “next” button in my table is: “example_next”.
Could you help me? Thanks in advance!
That’s my code:

<link rel=”stylesheet” href=”//cdn.datatables.net/1.10.5/css/jquery.dataTables.min.css” type=”text/css” />
<script src=”//code.jquery.com/jquery-1.11.1.min.js”></script>
<script src=”//cdn.datatables.net/1.10.6/js/jquery.dataTables.min.js”></script>
<script src=”//cdn.datatables.net/tabletools/2.2.4/js/dataTables.tableTools.min.js”></script>
<script src=”../../media/js/dataTables.editor.min.js”></script>

$(document).ready(function() {
$(‘#example’).dataTable( {

“pagingType”: “simple”,
“lengthMenu”: [[1], [1]],
“bSort”: false,

“language”: {
“paginate”: {
“previous”: true


} );

} );
$(“#button”).click(function () {

Field::inst( ‘answer’ )->validator( ‘Validate::notEmpty’ );

include( “/editor/php/DataTables.php” );


Editor::inst( $db, ‘survey_answers’ )
Field::inst( ‘answer’ )->validator( ‘Validate::required’ )
->process( $_POST )

$survey_id = $this->uri->segment(3);
$question_id = $this->uri->segment(4);
echo form_open(‘index/survey_fill/’ .$survey_id , $att); ?>
<table id=’example’ >
echo validation_errors();

foreach ($survey as $row)

<?php echo “$row->question”; ?><br/>
<?php echo “<input type=’hidden’ name=’question_id’ value=’$row->question_id’ />”; ?>

‘name’ => ‘answer[‘.$row->question_id.’]’,
‘value’ => ‘5’,
“data-val” => true,
“data-val-required” =>”Required field”

echo “<input type=’hidden’ name=’survey_id’ value=’$row->survey_id’>”;

echo form_radio($data);
echo ” 5 “;
‘name’ => ‘answer[‘.$row->question_id.’]’,
‘value’ => ‘4’,
echo form_radio($data);
echo ” 4 “;
<?php echo ‘<input type=”submit” id=”button” onsubmit=”return validate(this);” name = “submit” value=”Submit” class=”btn btn-success”>’;


to post a comment

0Be the first to comment 😎



Help @cat_girl spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 7.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ,
analytics: Fullres

legal: ({
terms: of use,
privacy: policy
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
recent_tips: (
tipper: Anonymous,
tipped: article
amount: 1000 SATS,

tipper: Anonymous,
tipped: article
amount: 1000 SATS,

tipper: @Nanditha,
tipped: article
amount: 1000 SATS,