SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot boognish's Avatar
    Join Date
    Sep 2005
    Location
    Leeds
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JS Validation conundrum

    Hi,

    I'm having trouble getting a particular validation script to function correctly. I need to validate whether either a textfield 'partner name' is filled in *or* a checkbox 'no partner' is checked. I tried with the script below but this only seems to validate the partner name textfiled. When the 'no partner' textbox is checked it comes up with the alert saying neither the checkbox or textfield is filled in. Can anyone help?

    Javascript...
    Code:
    <script type="text/javascript">
    function validate_email(field,alerttxt)
    {
    with (field)
      {
      apos=value.indexOf("@");
      dotpos=value.lastIndexOf(".");
      if (apos<1||dotpos-apos<2)
        {alert(alerttxt);return false;}
      else {return true;}
      }
    }
    
    function validate_required(field,alerttxt)
    {
    with (field)
      {
      if (value==null||value=="")
        {
        alert(alerttxt);return false;
        }
      else
        {
        return true;
        }
      }
    }
    
    function validate_date(field,alerttxt)
    {
    with (field)
      {
      if (value=="na")
        {
        alert(alerttxt);return false;
        }
      else
        {
        return true;
        }
      }
    }
    
    function validate_form(thisform)
    {
    with (thisform)
      {
    if ((validate_required(partner,"Partner field must be fully filled out *or* no partner checked!")==false)||(validate_required(nopartner,"Partner field must be filled out *or* no partner checked!")==false))
     {partner.focus();return false;}
      }
    }
    </script>
    Form html ...

    Code:
    Partner Name:<input type="text" name="partner" />
    No Partner:<input type="checkbox" name="nopartner">
    Thanks!

  2. #2
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Maybe this will help some:

    PHP Code:
    <form method="post">
        
    Partner Name:<input type="text" name="partner" />
        
    No Partner:<input type="checkbox" name="nopartner" />
        <
    input type="submit" value="Submit" />
    </
    form>
    <
    script type="text/javascript">
        
    window.onload = function() {
            var 
    frm document.forms[0];

            
    frm.onsubmit = function() {
                if( 
    frm.partner.value !== '' || frm.nopartner.checked === true ) {
                    
    alert('Validating');        
                    return 
    false;
                } else {
                    
    alert('You missed something...');
                }
            };
        };
    </script> 

  3. #3
    SitePoint Zealot boognish's Avatar
    Join Date
    Sep 2005
    Location
    Leeds
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks for your help on this.

    I finally figured it out with this...
    Code:
     else if((partner.value == '')&&(nopartner.checked == false))
    	 {   alert('Partner Name must be filled in *or* No Partner must be checked');  return false;}


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
  •