Reputation: 409
I have created a form with around 18 input fields. All the input fields contain 0 as the default value. I need to validate and prevent form submission and need to show an alert message if at least one of the default value won't be changed or updated by the user.
I'm using parsley validation to validate my form, so is it possible to achieve my requirement using parsley or javascript.
Thanks
Upvotes: 0
Views: 219
Reputation: 978
You can use same class name for this inputs and add event on submit form to check those inputs values, and then check if there is any input with default value.
For example:
//Elements with your input class
const formFields = document.getElementsByClassName('you-class');
//Add validation on event click
document.addEventListener('click', function (event) {
validate(event);
}, false);
//Simple function to validate
function validate(e) {
for (let i = 0; i < formFields.length; i++) {
if(formFields[i].value === 0) {
//block submit
e.preventDefault();
//display some error information...
}
}
}
Upvotes: 0