Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Can't find why it's not validating text fields

    I created a form and I have it linked to a Javascript validation file but it does not validate it. It seems that everything is correct but obviously I'm missing something. Can someone check it out, thanks.

    Here is the form:

    Code:
              <form name="free_dvd" id="free_dvd" method="post" action="/cgi-sys/formmail.pl">
              <input type="hidden" name="recipient" value="" />
              <input type="hidden" name="subject" value="Five Free DVDs" />
              <input type="hidden" name="env_report" value="REMOTE_HOST,REMOTE_ADDR,HTTP_USER_AGENT" />
                <p class="Verdana11pt_Plain_drak">
                  <label>First Name: <br />
                   <input type="text" name="first_name" id="first_name" size="40" maxlength="40" title="Type First Name Here" accesskey="f" />
                  </label>
                  *<br />
                  <label>Last Name: <br />
                    <input type="text" name="last_name" id="last_name" size="40" maxlength="40" title="Type Last Name Here" accesskey="l" />
                  </label>
                  *<br />
                  <label>ADDRESS:<br>
                    <textarea name="address" id="address" cols="45" rows="5"></textarea>
                    *<br />
              </form>
    Now here is the Javascript that I'm using to validate the text fields:

    Code:
    var validations = new Array();
    
    // Define which validations to perform. Each array item
    
    // holds the form field to validate, and the validation
    
    // to be applied. This is the only part you need to
    
    // customize in order to use the script in a new page!
    
    validations[0]=["document.free_dvd.first_name", "notblank"];
    
    validations[1]=["document.free_dvd.last_name", "notblank"];
    
    validations[2]=["document.free_dvd.address", "notblank"];
    
    
    // Customize above array when used with a new page.
    
    function isEmpty(s) 
    
    {
    
      if (s == null || s.length == 0)
    
        return true;
    
    
    
      // The test returns true if there is at least one non-
    
      // whitespace, meaning the string is not empty. If the
    
      // test returns true, the string is empty.
    
      return !/\S/.test(s);
    
    }
    
    
    
    function looksLikeEmail(field)
    
    {
    
      var s = field.value;
    
    
    
      if (isEmpty(s))
    
       {
    
         alert("Email may not be empty");
    
         field.focus();
    
         return false;
    
       }
    
    
    
      if (/[^@]+@\w+/.test(s))
    
           return true;
    
      alert("E-mail not in valid form.");
    
      field.focus();
    
      return false;
    
    }
    
    
    
    function isInteger(field)
    
    {
    
      var s = field.value;
    
      if (isEmpty(s))
    
       {
    
         alert("Field cannot be empty");
    
         field.focus();
    
         return false;
    
       }
    
    
    
      if (!(/^-?\d+$/.test(s)))
    
       {
    
         alert("Field must contain only digits");
    
         field.focus();
    
         return false;
    
       }
    
     return true;
    
    }
    
    
    
    function validate()
    
    {
    
      var i;
    
      var checkToMake;
    
      var field;
    
    
    
      for (i = 0; i < validations.length; i++)
    
       {
    
         field = eval(validations[i][0]);
    
         checkToMake = validations[i][1];
    
         switch (checkToMake)     
    
          {
    
           case 'notblank': if (isEmpty(field.value))
    
                              {
    
                               alert("Field may not be empty");
    
                               field.focus()
    						   
    						   //field.style.backgroundColor = "#AA0000"; 
    
                               return false;
    
                              }
    
                            break;
    
           case 'validemail':  if (!looksLikeEmail(field))
    
                                   return false;
    
                               break;
    
           case 'isnumber':  if (!isInteger(field))
    
                                return false;
    
          }
    
       }
    
      return true;
    
    }

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    You missed the onsubmit() in the form tag. Check the documentation for your validation script.
    Are you a Help Vampire?

  • Users who have thanked tomws for this post:

    surfandturfsd (06-15-2009)

  • #3
    New to the CF scene
    Join Date
    May 2009
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    That was it, thanks.


  •  

    Posting Permissions

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