Connecting Tech Pros Worldwide Forums | Help | Site Map

about JavaScript Form Validations ...

Newbie
 
Join Date: Oct 2006
Posts: 8
#1: Nov 16 '06
Hello Experts,

in the following link there is fine tool for javascript client-side form validation,
as some of you I guess/hope know:

http://www.javascript-coder.com/html-form/javascript-form-validation.phtml

I just found it and experiment with it a little. Problem now is that I couldn't find out how to validate radiobuttons with it, i.e., if the client has selected a radiobutton from the set of them. It seems to work well if you are using text or textarea fields. There were no examples of radiobuttons or checkbuttons though.

Is there anyone here who knows the tool concerned, and could tell me how to validate ratiobuttons and also checkboxs with it?

Expert
 
Join Date: Oct 2006
Location: NC
Posts: 1,722
#2: Nov 16 '06

re: about JavaScript Form Validations ...


Radio Buttons:

[html]<form name="myform">
<input type="radio" value="1st value" name="myradiobutton" />1st<br />
<input type="radio" value="2nd value" name="myradiobutton" />2nd<br />
<input type="radio" value="3rd value" name="myradiobutton" />3rd<br />&nbsp;<br />
<input type="submit" name="submitit" onclick="valbutton(myform);return false;" value="Validate" />
<input type="reset" name="reset" value="Clear" />
</form>[/html]

Javacript:
Expand|Select|Wrap|Line Numbers
  1. function valbutton(thisform) {
  2. // place any other field validations that you require here
  3. // validate myradiobuttons
  4. myOption = -1;
  5. for (i=thisform.myradiobutton.length-1; i > -1; i--) {
  6. if (thisform.myradiobutton[i].checked) {
  7. myOption = i; i = -1;
  8. }
  9. }
  10. if (myOption == -1) {
  11. alert("You must select a radio button");
  12. return false;
  13. }
  14.  
  15. alert("You selected button number " + myOption
  16. + " which has a value of "
  17. + thisform.myradiobutton[myOption].value);
  18.  
  19. // place any other field validations that you require here
  20. thisform.submit(); // this line submits the form after validation
  21. }
Checkboxes are pretty much the same way.

HTH,
Aric
Newbie
 
Join Date: Oct 2006
Posts: 8
#3: Nov 17 '06

re: about JavaScript Form Validations ...


Thank you AricC,

very much for the example code - works fine!

Regards, MY


Quote:

Originally Posted by AricC

Radio Buttons:

[html]<form name="myform">
<input type="radio" value="1st value" name="myradiobutton" />1st<br />
<input type="radio" value="2nd value" name="myradiobutton" />2nd<br />
<input type="radio" value="3rd value" name="myradiobutton" />3rd<br />&nbsp;<br />
<input type="submit" name="submitit" onclick="valbutton(myform);return false;" value="Validate" />
<input type="reset" name="reset" value="Clear" />
</form>[/html]

Javacript:

Expand|Select|Wrap|Line Numbers
  1. function valbutton(thisform) {
  2. // place any other field validations that you require here
  3. // validate myradiobuttons
  4. myOption = -1;
  5. for (i=thisform.myradiobutton.length-1; i > -1; i--) {
  6. if (thisform.myradiobutton[i].checked) {
  7. myOption = i; i = -1;
  8. }
  9. }
  10. if (myOption == -1) {
  11. alert("You must select a radio button");
  12. return false;
  13. }
  14.  
  15. alert("You selected button number " + myOption
  16. + " which has a value of "
  17. + thisform.myradiobutton[myOption].value);
  18.  
  19. // place any other field validations that you require here
  20. thisform.submit(); // this line submits the form after validation
  21. }
Checkboxes are pretty much the same way.

HTH,
Aric

Reply