I have two forms on my website which must be validated and I am torn between many options. After a bit of research, I am not so keen on using jquery as it only displays error messages either before or after the input fields. What I am trying to achieve is to display the error messages in a separate div which I created right next to the submit button. I have 4 fields in total (name, email, phone and message) and the only method I have tried was the onblur function. It gives user a some sort of indication of missing field but the form still submits itself. As you can tell, I am not super competent in JS so I would really appreciate your advice on how to best solve this issue. Many thanks in advance.
document.my_form.name.onblur=function() {
if (document.my_form.name.value==="") {
document.getElementById('response_one').innerHTML="! Please enter your name.</br>";
} else {
document.getElementById('response_one').innerHTML="";
}
}
There are a few different ways to validate a form. If I may make one suggestion, though. Donāt use an alert on each blur - that can become quite annoying to the user.
Create a validation function that will be called upon the form submit, one that will declare a variable and set it to blank, then for each field that doesnāt pass muster add text and a line break character. Then, at the end of the function, if the variable is empty, all fields are correct, empty the error element of all text and submit the form - but if the variable isnāt empty, populate the error element with the data in the variable and return false so the form doesnāt submit.
The last field on my form is not an input field but a text area. Your foreach function is only looping through the input fields. How can I add the text area to the loop?
If you do come back to considering using the jQuery validator, it can easily place the errors in your error label container. With a bit of further research a full and complete solution can be put together.
I have one last question for you. Can I use this script another form on the same page. I have two forms but only one of them is visible at a time (I put them in a slider) if I wanted to use this script for the second form, what should I modify?
I have tried changing the class of submit button for the second form and duplicated the script but that did not work. I would really appreciate if you could spare your opinion on this.
I wanted to ask you one last question if you donāt mind. Does .parent selector only work when all the elements placed inside the same div? For example when I place the submit button into a right aligned div and error window into a left aligned div, the script doesnā t work. However, when I put all of them into the same div, it just works fine.
I am using Foundation5 by the way, to layout the elements I need to use separate columns.
I guess .each is only looping the input fields but omitting the select fields. Even though I set required attribute for select fields, they are still not getting included in the errors array. Is there a quick fix for this?