By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,528 Members | 1,324 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,528 IT Pros & Developers. It's quick & easy.

about JavaScript Form Validations ...

P: 8
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?
Nov 16 '06 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 1,892
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
Nov 16 '06 #2

P: 8
Thank you AricC,

very much for the example code - works fine!

Regards, MY


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
Nov 17 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.