0

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

SethuNagaKarthik
  • 387
  • 1
  • 3
  • 17

1 Answers1

0

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...
        }
    }
}
Michał Lipa
  • 968
  • 2
  • 12
  • 22
  • thanks for your response but inside the validate function. You are looping the formField to check for value === 0. I think instead of that, I have to get all the input value and need to check if anyone is not equal to zone. – SethuNagaKarthik Oct 17 '18 at 13:05