How do I validate my form field after the checkbox is checked?

I have created one form with dynamically generated fields. and I have one checkbox with a unique ID. when the user clicks this checkbox, only those two fields ("name and age") are displayed. after clicking only the "age" field needs to be checked.

here is my code:

$(document).ready(function() {
    $('#person').click(function() {
        function formValidator(){
            var age = document.getElementsByName('age[]');
            for (var i = 0; i< age.length; i++) {
                if(!isNumeric(age[i], "Please enter a valid Age")){
                    return false;
                }
            }
            return true;
        }

        function isNumeric(elem, helperMsg){
            var numericExpression = /^[0-9]+$/;
            if(elem.value.match(numericExpression)){
                return true;
            } else {
                alert(helperMsg);
                elem.focus();
                return false;
            }
        }
   });
});

$(document).ready(function() {
    $('#person').click(function() {
        $('#name').attr('required','required');
        $('#age').attr('required','required');
    });
});

      

Style

:

.selectContainer{
    display:none;
 }

input[type=checkbox]:checked ~ .selectContainer {
    display:block;
}

      

HTML code:

<form action="" method="post" onSubmit="return formValidator()">
    <label for="name">Any Accompanying Person ?:</label>
    <input  type="checkbox" name="person" id="person" >Yes
    <div class="selectContainer">
        <br>
        <label>Person Details</label>
        <p>
            <div style="padding-left:70px;"> 
                <input type="button" value="Add Person" onClick="addRow('dataTable')" /> 
                <input type="button" value="Remove Person" onClick="deleteRow('dataTable')" /> 
            </div>
        </p>
        <table style="padding-left:50px;" id="dataTable" class="form" border="1"  >
            <tbody>
                <tr>
                    <p>
                        <td><input type="checkbox" name="chk[]" checked="checked" /></td>
                        <td>
                            <label>Name</label>
                            <input type="text" size="20" name="name[]" id="name"   >
                        </td>
                        <td>
                            <label>Age</label>
                            <input type="text" size="20" name="age[]" id="age"  >
                        </td>
                    </p>
              </tr>
           </tbody>
       </table>
       <div class="clear"></div>
       </fieldset>
   </div>
</div>
<h3>Choose Your Payment Option</h3>
<h1>
<div style="padding-left:150px;">
    <input type="radio" name="type" value="visa">VISA/MASTER CARD:<br />
    <input type="radio" name="type" value="cheque"> CHEQUE/DEMAND DRAFT<br />
    <input type="radio" name="type" value="neft">NEFT<br /><br/>
</div>
<label></label>
<input type="submit" name="submit" value="submit"><br />
</form>

      

problem: the form field "age" passes validation by clicking the ("Any Accompanying Person ?:") checkbox. the problem is that the user tries to submit the form without clicking on that checkbox and then everything to ask her to submit. how to get a greeting for this? please, help

+3


source to share


2 answers


The validator resides in a click handler that should live outside of this (in the base on document.ready ()).

Also if you just want to check when this checkbox is checked, you can check it in javascript and select it using the checkbox name (if it has a unique ID every time).



function formValidator(){
    var age = document.getElementsByName('age[]');
    if($("input[name = 'chk[]']").prop('checked')){
        for (var i = 0; i< age.length; i++) {
            if(!isNumeric(age[i], "Please enter a valid Age")){
                return false;
            }
        }
    }
    return true;
}

      

0


source


Bring all javascript functions outside of click events. Try this form. Validator function



function formValidator(){

        if($("#person").is(":checked")) {

   var age = document.getElementsByName('age[]');
    for (var i = 0; i< age.length; i++) {
  if(!isNumeric(age[i], "Please enter a valid Age")){
    return false;
  }
    }

        }

 return true;
     }

      

0


source







All Articles