To find out which value (if any) has been selected, you need to loop through the array until you find which one has been selected: defines a locally scoped variable.
If you're working with arrays of checkboxes to submit data to a server-side script then you might already have some grey hairs from trying to figure out how to validate the input using Java Script.
To take things a step further you can even explore Ajax Form Validation which lets you supply real-time feedback using server-side scripts triggered by Java Script events.
The team at have made a video based on this article as a part of their Java Script training lessons which you can view below. Would be great if you could post full examples of code including form and not just pieces that fit into the greater framework of code.
I tested your first piece of code on a form and it does not work at all.
Tried setting it up on onsubmit on both form and button and no go.
My problem is that when i click on the submit button, for correct integer input, it is working. Can someone help me out with what the problem is and what is that i should modify?
Validating form input with Java Script is easy to do and can save a lot of unnecessary calls to the server.
But if the user unchecks the box, the user DOESNT have to enter info in textarea.
Create three files and name them: // SELECTING ALL TEXT ELEMENTS var username = document.forms['vform']['username']; var email = document.forms['vform']['email']; var password = document.forms['vform']['password']; var password_confirm = document.forms['vform']['password_confirm']; // SELECTING ALL ERROR DISPLAY ELEMENTS var name_error = Element By Id('name_error'); var email_error = Element By Id('email_error'); var password_error = Element By Id('password_error'); // SETTING ALL EVENT LISTENERS Event Listener('blur', name Verify, true); Event Listener('blur', email Verify, true); Event Listener('blur', password Verify, true); // validation function function Validate() // event handler functions function name Verify() function email Verify() function password Verify() And that's all about the code.
Now to run this, copy the code into a folder inside htdocs or www folder of your PHP installation and log unto you browser and run it.
Otherwise a browser with Java Script disabled, or a hacker trying to compromise your site, can easily by-pass client-side validation.
For an alternative approach to client-side form validation, without Java Script, check out our new article on HTML5 Form Validation which is available now in most modern browsers. The first test in the example is therefore only necessary in order to provide a different error message when the input is blank.In more complicated forms you will want to set conditions on the form that combine multiple elements.