SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Dec 2001
    Location
    Vancouver, Canada
    Posts
    352
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    The form won't validate, can someone spot the mistake?

    The site is not yet live, so I added the codes on one of my website: http://www.visual-directory.com/contact-us-test.php

    Here is the code to make it easy on you:

    HTML Code:
    <script>
    function validateIt(theForm)
    {
    //InquiryForSelf
    if(theForm.InquiryForSelf.selectedIndex==0)
    {
    	 alert("Please tell us whether this request is for you");
    theForm.InquiryForSelf.focus(); 
    return (false);
    }
     
    //YourFirstName
    if(theForm.YourFirstName.selectedIndex==0)
    {
    	 alert("Only your first name is required");
    theForm.YourFirstName.focus(); 
    return (false);
    }
     
    //Phone
    if(theForm.Phone.selectedIndex==0)
    {
    	 alert("Your Telephone # is kept Private but is required");
    theForm.Phone.focus(); 
    return (false);
    }
    //TimeToCall
    if(theForm.TimeToCall.selectedIndex==0)
    {
    	 alert("When is the best time to call?");
    theForm.TimeToCall.focus(); 
    return (false);
    }
    	//email
    if(document.tafform.from_email.value=="")
    {
    	 alert("Enter email.");
    theForm.from_email.focus(); 
    return (false);
    }
    else
    {
    	 var str=document.tafform.from_email.value;
    var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
    if (filter.test(str))
    return true;
    else
    {
    alert("Please input a valid email address!")
    return false;
    }
     
    } 
     
    return (true);
    }//end function
    //DescribeSituation
    if(theForm.DescribeSituation.selectedIndex==0)
    {
    	 alert("Please provide us with a brief overview of the situation");
    theForm.DescribeSituation.focus(); 
    return (false);
    }
     
    }
    </script>
    Then on the form itself, I have:

    HTML Code:
     <FORM action="http://www.example.com/cgi-bin/tafform/sendit.cgi" method="POST" name="tafform" ID="tafform" onSubmit="return validateIt(this)">
    When I try the form, it doesn't validate NOTHING. Can someone figure this out. My mind is going to mush.

  2. #2
    SitePoint Zealot yacka's Avatar
    Join Date
    Aug 2006
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I suspect you have a misplaced curly brace, indent your code so you can see everything aligned properly. Also you are checking the selectedIndex on some fields that do not have that property.

  3. #3
    SitePoint Zealot yacka's Avatar
    Join Date
    Aug 2006
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, that whole bit under //DescribeSituation is not contained within a function. Also (I might cop some flack for this), learn to write code with only a single exit point, it's easier to test and validate.


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
  •