Reputation: 11
I am trying to apply formValidation.io framework to validate input fields in my form. But to do this, I do not want to mention names of my input fields before hand, rather I want to identify all the input fields of type 'text' present inside my form '#myform' and then pass list of input element names to formValidation() method of formValidation.io
Could you please help me to achieve this, I can summarize the same in below two steps:
For example, consider below:
<form id="myForm">
<input type="text" name="accountnumber" />
<input type="text" name="forename" />
</form>
JavaScript:
$('#myForm').formValidation({
framework: 'foundation',
fields: {
forename: {
validators: {
notEmpty: {
...
}
}
},
accountnumber: {
validators: {
notEmpty: {
...
}
}
}
}
});
As per above code sample, I want to
Identify input elements (forename and accountnumber) of type 'text' inside form id '#myform' using jquery
Pass forename and accountnumber input fields to formValidation() method of formValidation.io at runtime. I do not want to mention these names in hard-coded fashion in the script. Rather, I want these input fields names to be given to formValidation() method at the specified place at runtime for each input field. (I supppose if any looping can be done for this, but not sure how)
Apply notEmpty validation on all the fields.
Validations should work for all the identified fields.
Kindly help me to resolve this problem. Thanks in advance.
Regards, Deepak
Upvotes: 1
Views: 954
Reputation: 26
Use addField to add fields to the validator
$('#Form').formValidation('addField', 'nameOfTheField', {
validators: {
//add validator for this field
}
});
https://formvalidation.io/guide/api/add-field/
Upvotes: 1