Results 1 to 5 of 5

Thread: JavaScript form validation woes

  1. #1
    Join Date
    Oct 2008
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default JavaScript form validation woes

    Desperately seeking help with a little script. I'm useless with JS
    This is the last function in my script. Anyone know why it would not throw up the error when the radio box with name 'method' and value 'fax' is checked and the input with name 'vetfax' is empty?

    Code:
    function DoCustomValidation()
    {
      var frm = document.forms["request"];
      if(frm.method.value=="fax" && frm.vetfax.value=="")
      {
        sfm_show_error_msg('Please provide a fax number');
        return false;
      }
      else
      {
        return true;
      }
    }
    Last edited by thegoose; 08-04-2009 at 10:55 PM.

  2. #2
    Join Date
    Jul 2008
    Posts
    40
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default

    Hi thegoose,

    try this simple variation:

    Code:
    var DoCustomValidate = function() {
    var frm = request || document.forms.request;
       if ( frm.method.value !== "fax" && frm.method.value ) {
          return true;
       } alert("Please provide a fax number");
       return false;
    };

  3. The Following User Says Thank You to rainarts For This Useful Post:

    thegoose (08-05-2009)

  4. #3
    Join Date
    Oct 2008
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Thanks rainarts. That didn't work. There must be something somewhere else that's broken too. Appreciate the help.

  5. #4
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    just guessing without seeing your page but

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script  type="text/javascript">
    /*<![CDATA[*/
    function DoCustomValidation(){
     var pass=true,frm=document.forms["request"],rads=frm.method
     for (var z0=0;z0<rads.length;z0++){
      if (rads[z0].checked&&frm['vet'+rads[z0].value].value.replace(/\s/g,'').length==0){
       alert('Please provide a '+rads[z0].value+' number');
       pass=false;
      }
     }
     return pass;
    }
    
    
    /*]]>*/
    </script></head>
    
    <body>
    <form name="request"  >
    Fax <input type="radio" name="method" value="fax" /><input name="vetfax" /><br />
    Post <input type="radio" name="method" value="post" /><input name="vetpost" /><br />
    <input type="button" name="" value="TEST" onclick="DoCustomValidation()"/>
    </form>
    </body>
    
    </html>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  6. #5
    Join Date
    Nov 2008
    Posts
    58
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Default

    To check whether the radio button with the given value is selected, you have to loop through the radio buttons like this:

    Code:
    if(objRadio.length)
     {
         for(var c=0;c < objRadio.length;c++)
         {
           if(objRadio[c].checked == "1" && 
    	     objRadio[c].value == value)
           {
            return true; 
           }
         }
     }
     else
     {
      if(objRadio.checked == "1" )
       {
        return true; 
       }    
     }

    I guess you are using the JavaScript Form Validation script here.

    There is a fourth parameter for the addValidation() function.
    The fourth parameter is the condition in which the validation should trigger.

    Try:
    myformValidator.addValidation("vetfax","req","Please fill in vetfax","VWZ_IsChecked(document.myform.elements[\"method\"],\"fax\")");

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •