Code for validating radio button in javascript

-1; i--) { if (thisform.myradiobutton[i].checked) { my Option = i; i = -1; } } if (my Option == -1) { alert("You must select a radio button"); return false; } alert("You selected button number " + my Option + " which has a value of " + thisform.myradiobutton[my Option].value); // place any other field validations that you require here thisform.submit(); // this line submits the form after validation }It should be a relatively simple matter for you to take the above form field definitions and javascript and incorporate it into your form.

Note that the javascript does not require amending to handle as many radio buttons within the same group as you require, you simply assign them all the same name within the form.

The setup and validation of radio buttons appears to be the form field that gives many webmasters the most difficulty in setting up.

So you want to put radio buttons on a form on your page but don't know how to go about validating that one has been selected or determining which one has been selected.

Well here we have a form containing three radio buttons to show you how to do it. Note that the highlighted parts of this code will be referenced within the validation javascript, you can call them what you like but you must name fields consistently (ie.

Since then developments in Java Script have introduced ways to perform radio button validation in far less obtrusive ways however the less obtrusive code is slightly more complicated and has a significant overhead if you do not use that approach for all of your field validations (as you ought to).

Of course many people still use this means of validating radio buttons simply because the code is easier for them to understand.

all fields shown highlighted here and in the javascript with the same name must still all have the same name if you rename them.

The javascript that this form uses to validate that a radio button has been selected follows.

china-autonews.ru

78 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>