Form validation tweak

Discussion in 'JavaScript' started by Jon12345, Jul 30, 2009.

  1. #1
    I have some code that tests a couiple of things on my form. Now I have added a catpcha field and the value required to pass will always be 4. So, the surfer has to enter 4 into the catpcha field.

    How do I modify the following code to do this? The catpcha field is called catpcha. :)

    <script language="JavaScript">
    
    function validate_required(field,alerttxt)
    {
    with (field)
    {
      if (value==null||value==""||value=="Select")
      {
      alert(alerttxt);return false;
      }
      else
      {
      return true;
      }
    }
    }
    
    function validate_form(thisform)
    {
    with (thisform)
    {
    if (validate_required(email,"Email must be filled out!")==false)
      {email.focus();return false;}
    
    else if 
    (validate_required(country,"Country must be filled out!")==false)
      {country.focus();return false;}
    }
    }
    </script>
    Code (markup):
    Thanks,

    Jon
     
    Jon12345, Jul 30, 2009 IP
  2. Martinoes

    Martinoes Peon

    Messages:
    110
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You should always do such kind of validations also on your server side.


    <script language="JavaScript">
    
    function validate_required(field,alerttxt)
    {
    with (field)
    {
      if (value==null||value==""||value=="Select")
      {
      alert(alerttxt);return false;
      }
      else
      {
      return true;
      }
    }
    }
    
    function validate_equals(field,fieldValue,alerttxt)
    {
    with (field)
    {
      if (value==null||value==""||value!= fieldValue)
      {
      alert(alerttxt);return false;
      }
      else
      {
      return true;
      }
    }
    }
    
    function validate_form(thisform)
    {
    with (thisform)
    {
    if (validate_required(email,"Email must be filled out!")==false)
      {email.focus();return false;}
    
    else if 
    (validate_required(country,"Country must be filled out!")==false)
      {country.focus();return false;}
    
    else if 
    (validate_equals(catpcha,4,"Incorrect catpcha value!") == false)
     {catpcha.focus(); return false;}
    }
    }
    </script>
    PHP:
     
    Martinoes, Jul 31, 2009 IP
  3. olddocks

    olddocks Notable Member

    Messages:
    3,275
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    215
    #3
    olddocks, Aug 6, 2009 IP
  4. morehawes

    morehawes Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Serverside validation is essential but having a user friendly prompt before submission is always nice.
     
    morehawes, Sep 11, 2009 IP