PDA

View Full Version : Recognize Incorrect E-mail in a form?



waitwhat
07-05-2009, 02:40 PM
Hi, I have a form that only has a few inputs, like Name, phone number and e-mail address. Now, if you type letters into the phone number input box and try to submit, it will recognize that you have not entered a valid phone number. But not the same for the e-mail address field. I just type in a few random letters, and it accepts it. Can someone please help?

Here is my javascript code:


function checkquotevalues(){
message="";
if(document.referaclient.name.value.length ==0){
message = message+"Please enter a First Name.\n";
}

if(document.referaclient.areacode.value.length != 3 || document.referaclient.exchange.value.length != 3 || document.referaclient.phonenumber.value.length != 4){
if(IsNumeric(document.referaclient.areacode.value) != 1 || IsNumeric(document.referaclient.exchange.value) != 1 || IsNumeric(document.referaclient.phonenumber.value) != 1){
message = message+"Please enter a Numeric Phone Number.\n";
}
else { message = message+"Please enter a Phone Number.\n";}
}
else{
if(IsNumeric(document.referaclient.areacode.value) != 1 || IsNumeric(document.referaclient.exchange.value) != 1 || IsNumeric(document.referaclient.phonenumber.value) != 1){
message = message+"Please enter a Numeric Phone Number.\n";
}
}

if(document.referaclient.exchange.value == 555){
message = message+"Please enter a Valid Phone Number.\n";
}

if (document.referaclient.Email.value.length ==0){
message = message+"Please enter your Email Address.\n";
}
else {
checkemail(document.referaclient.Email.value)
}

if(document.referaclient.businessname.value.length ==0){
message = message+"Please enter a Business Name.\n";
}

if(document.referaclient.contactname.value.length ==0){
message = message+"Please enter a Contact Name.\n";
}

if(document.referaclient.clientphonenumber.value.length ==0){
message = message+"Please enter the Client's Phone Number.\n";
}

if (message.length > 0){
alert(message);
return false;
}

return true;
}



function checkemail(str){
var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
if (filter.test(str))
testresults=true
else{
message = message + "Email address seems incorrect (check @ and .'s)\n"
return false;
}
}

function IsNumeric(sText)
{
var ValidChars = "0123456789";
var IsNumber=true;
var Char;


for (i = 0; i < sText.length && IsNumber == true; i++)
{
Char = sText.charAt(i);
if (ValidChars.indexOf(Char) == -1)
{
IsNumber = false;
}
}
return IsNumber;
}


Here is a link to my .asp code: ASP page (http://www.wuzhannanan.com/backend/surechoice-refer.asp)

And, since the page is not up live, here is a screen shot of the code you're looking at:

http://www.wuzhannanan.com/backend/referral_form.gif

Thank you for any help you can give me!

sniperman
07-08-2009, 08:23 AM
Your javascript code does a bunch of checks and balances. Each one employs either an array of values to include or to exclude and cross-checks these arrays to return a value.

The example below from your script checks the length of the email typed in. IF the email is equal to zero characters long (no input) the statement is true and runs the argument.


if (document.referaclient.Email.value.length ==0){
message = message+"Please enter your Email Address.\n";
}
else {
checkemail(document.referaclient.Email.value)
}



I did however detect in your checkemail() function a problem. Each variable declared should have opening and closing quotation marks " " and yours doesn't. var filter="whatever the characters to be parsed by another function in here"; is the correct format.




function checkemail(str){ var filter="/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i";

if (filter.test(str))
testresults=true

Try that and see what happens.