...

View Full Version : user survey form validation



helen11
12-16-2011, 09:49 AM
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.


<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>

Dormilich
12-16-2011, 11:41 AM
remove <!--, that should cause a JS syntax error (and it's not needed anyways)

Philip M
12-16-2011, 11:52 AM
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. :rolleyes: In this case newNumber.

helen11
12-16-2011, 12:54 PM
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??

Philip M
12-16-2011, 01:06 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum