about JavaScript Form Validations ...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mylo1968
    New Member
    • Oct 2006
    • 8

    about JavaScript Form Validations ...

    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.phtm l

    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?
  • AricC
    Recognized Expert Top Contributor
    • Oct 2006
    • 1885

    #2
    Radio Buttons:

    [html]<form name="myform">
    <input type="radio" value="1st value" name="myradiobu tton" />1st<br />
    <input type="radio" value="2nd value" name="myradiobu tton" />2nd<br />
    <input type="radio" value="3rd value" name="myradiobu tton" />3rd<br />&nbsp;<br />
    <input type="submit" name="submitit" onclick="valbut ton(myform);ret urn false;" value="Validate " />
    <input type="reset" name="reset" value="Clear" />
    </form>[/html]

    Javacript:
    Code:
    function [b]valbutton[/b](thisform) {
    // place any other field validations that you require here
    // validate myradiobuttons
    myOption = -1;
    for (i=thisform.myradiobutton.length-1; i > -1; i--) {
    if (thisform.[b]myradiobutton[/b][i].checked) {
    myOption = i; i = -1;
    }
    }
    if (myOption == -1) {
    alert("You must select a radio button");
    return false;
    }
    
    [i]alert("You selected button number " + myOption
    + " which has a value of "
    + thisform.myradiobutton[myOption].value);[/i]
    
    // place any other field validations that you require here
    thisform.submit(); // this line submits the form after validation
    }
    Checkboxes are pretty much the same way.

    HTH,
    Aric

    Comment

    • mylo1968
      New Member
      • Oct 2006
      • 8

      #3
      Thank you AricC,

      very much for the example code - works fine!

      Regards, MY


      Originally posted by AricC
      Radio Buttons:

      [html]<form name="myform">
      <input type="radio" value="1st value" name="myradiobu tton" />1st<br />
      <input type="radio" value="2nd value" name="myradiobu tton" />2nd<br />
      <input type="radio" value="3rd value" name="myradiobu tton" />3rd<br />&nbsp;<br />
      <input type="submit" name="submitit" onclick="valbut ton(myform);ret urn false;" value="Validate " />
      <input type="reset" name="reset" value="Clear" />
      </form>[/html]

      Javacript:
      Code:
      function [b]valbutton[/b](thisform) {
      // place any other field validations that you require here
      // validate myradiobuttons
      myOption = -1;
      for (i=thisform.myradiobutton.length-1; i > -1; i--) {
      if (thisform.[b]myradiobutton[/b][i].checked) {
      myOption = i; i = -1;
      }
      }
      if (myOption == -1) {
      alert("You must select a radio button");
      return false;
      }
      
      [i]alert("You selected button number " + myOption
      + " which has a value of "
      + thisform.myradiobutton[myOption].value);[/i]
      
      // place any other field validations that you require here
      thisform.submit(); // this line submits the form after validation
      }
      Checkboxes are pretty much the same way.

      HTH,
      Aric

      Comment

      Working...