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 5 of 5
  1. #1
    New Coder
    Join Date
    Nov 2011
    Location
    South Africa
    Posts
    60
    Thanks
    14
    Thanked 0 Times in 0 Posts

    user survey form validation

    Hi I have created a form and I want the user’s information to be display in a textarea, also be emailed to a predefined email address but I cant get it working!! Any help would be greatly appreciated.

    Code:
    <script type="text/javascript">
      <!--
    function validEmail()
    {
        if (!document.userSurvey.eMail.value) 
        {
            alert("E-mail Address missing. Please enter a valid E-mail address to continue.");
            document.userSurvey.eMail.focus();
            return false;
        }
        else
        {
            var emailAddress = document.userSurvey.eMail.value;  //3. complete this statement
            var atLoc = emailAddress.indexOf("@",1);
            var dotLoc = emailAddress.indexOf(".",atLoc+2);
            var len = emailAddress.length;
    
            if (atLoc > 0 && dotLoc > 0 && len > dotLoc+2)
            {
                return true;
            }
            else
            {
                alert("Invalid E-mail address! Please enter your e-mail address again.");
                document.userSurvey.eMail.focus();
                return false;
            }
        }
    }
    
    function validNo()
    {
        if (!document.userSurvey.phone.value)
        {
            alert("Phone number missing. Please enter a valid phone number to continue.");
            document.userSurvey.phone.focus();
            return false;	  
        }
        else
        {
            var numbersOnly = "";
            var chars = "";
            var phoneNo = document.userSurvey.phone.value;
    
            for (i = 0; i < phoneNo.length; i++)
            {
                chars = phoneNo.substring(i,i+1);
                
                 
                if (chars >= "0" && chars <= "9")
                {
                    numbersOnly = numbersOnly + chars;
                }
            }
    
            if (numbersOnly.length != 13)
            {
                alert("Incorrect phone number format.You must enter 13 numbers.");
                document.userSurvey.focus();
                return false;
            }
            else
            {
                var areacode = numbers.substring(0,2);
                var leading0 = numbersOnly.substring(2,3);
                var exchange = numbersOnly.substring(3,5);
                var ext1 = numbersOnly.substring(5,9);
                var ext2 = numbersOnly.substring(9);
                var newNumber =( "+" + areacode + " " +"(" + leading0 + ")" + exchange + " " + ext1 + "-" + ext2);
                
                
    		document.userSurvey.phone.value = reformattedNumber;
                return true;
            }
        }
    }   
    
    function validName()
    {
        if (!document.userSurvey.userName.value)
        {
            alert("Name not entered. Please enter your name to continue.");
            document.userSurvey.userName.focus();
            return false;
        }
        else
        {
            return true;
        }
    }
    
    function validChoice() 
    {
        var bookChoice = "";
        var x= "";
    
    	for (i=0;i< 4;i++)
        {
            if (document.userSurvey['bookChoice'+i].checked)
            {
                bookChoice = document.userSurvey['bookChoice'+i].value;
                x = x +"\n"+ bookChoice;		//"\n" output a newline
            }
        }
    
        if (bookChoice == "")
        {
            alert("You must select at least one book category.");
            return false;
        }
        else
        {
            var userName = document.userSurvey.userName.value;
            var eMail = document.userSurvey.eMail.value;
            var phoneNo = document.userSurvey.phone.value;
    	
    
    	var combined = userName + "\n" + eMail + "\n" + phoneNo + "\n" + bookChoice + "\n";
    	document.getElementById("surveyResults").value = combined; 
            
            
            
            return true;
        }
    }
    
    function validInput()
    {
        if(validName())&&(validEmail())&&(validNo())&&(validChoice())) 
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    
      //--> 
      </script>

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,394
    Thanks
    13
    Thanked 353 Times in 349 Posts
    remove <!--, that should cause a JS syntax error (and it's not needed anyways)
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,150
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by helen11 View Post
    Hi I have created a form and I want the user’s information to be display in a textarea, also be emailed to a predefined email address but I cant get it working!! Any help would be greatly appreciated.
    Do you mean emailed using mailto:?

    The trouble with using this long-obsolete method (mailto) to send form results is its unpredictability. The method it is highly dependent on the browser in use and the email client in use (some people have only Yahoo, Gmail or Hotmail). In particular, your visitor must have Outlook or Outlook Express as the default client for this to work correctly. Even if your visitor is using Internet Explorer, but the default mail client is different (e.g. Eudora or Thunderbird), your mailto form will not work. With all of the browser troubles, you're likely to lose about half of your users' messages. Most of the email clients that can successfully send a mail will prompt the user with a somewhat threatening security dialog prior to sending - this can scare many users from continuing. Other users will not wish to reveal their email address. Also, what about people with Javascript disabled?

    In addition, if you place an unobfuscated email address in your webpage, the bots will quickly find it and inundate you in spam.

    In fact few browsers these days accept mailto: as a form action. You need a really old browser (<IE6) for it to work as more modern browsers simply
    open the email program (if any) and ignore the form. If you are going to use a form then use a server-side CGI formmail script as the action - there are
    several good free ones out there.

    document.userSurvey.phone.value = reformattedNumber;
    I don't see any variable named reformattedNumber. I gave you this in a previous post - but naturally you are expected to alter reformattedNumber to whatever is the actual name of the variable you are using. In this case newNumber.
    Last edited by Philip M; 12-16-2011 at 11:57 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    New Coder
    Join Date
    Nov 2011
    Location
    South Africa
    Posts
    60
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Yes using mailto, thats the option that has to be used. Do u know how i'd get the users information to be displayed in the textarea??

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,150
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by helen11 View Post
    Yes using mailto, thats the option that has to be used. Do u know how i'd get the users information to be displayed in the textarea??
    We seem to be going round in circles. I answered that at
    http://www.codingforums.com/showthread.php?t=246418
    Post #4.

    One more time, mailto: is no longer a viable option for sending form results. OK? Forget the idea.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Tags for this Thread

    Posting Permissions

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