...

View Full Version : Email Validation Question. :)



Wing99
12-05-2006, 02:38 PM
Hi Guys/Gals

im trying to code some error checking and all has been going well, but i have come to a pointwere is need to make sure the Email address entered is actually valid EG "abc@123.com" instead of gibberish. eg "dsfsfsfsfd"

i have taken this from a previous Enquiry form that my boss created but i doesn't appear to work, which is intresting becuase he thought it did. so either i have deleted a little bit of it, ( which i doubt as its still the same as before) or it didnt work in the first place. if someone could please give me a little advice i would really appreicate it.

****************MY CODE***************
function test(src) {
var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
return regex.test(src);
}
**************************************

Thank you in advance

Stuart

Ancora
12-05-2006, 03:05 PM
Stuart:

The problem might be the function name. test, is a javascript reserved word. You can see that it is, because you are using the test function.

src is also a JavaScript reserved word.

Change it to:
function testEmail(emailStr)

Also, those parentheses around the second part really aren't needed:

/^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;

Wing99
12-05-2006, 03:23 PM
Sorry i should of given you more info heres a bit more

<form action="ISCpostmail.asp" method="post" name="email" id="email" onSubmit =return checkForm this)>

*********Loads of Code i dont think is needed for this question.*********

******if you do need to see more code please ask ^.^ thanks alot*****

<SCRIPT LANGUAGE="JavaScript">

function checkForm(form) {
var buildstr;
var testresult

if (form.First_Name.value =="")
{
alert("Please enter your first name");
form.First_Name.focus();
return false;
}
if (form.Surname.value =="")
{
alert("Please enter your Surname");
form.Surname.focus();
return false;
}
if (form.Address.value =="")
{
alert("Please enter your Address");
form.Address.focus();
return false;
}
if (form.Town.value =="")
{
alert("Please enter your Town");
form.Town.focus();
return false;
}
if (form.Postcode.value =="")
{
alert("Please enter your Postcode");
form.Postcode.focus();
return false;
}

if (form.EmailAddress.value != "")
{
testresult = test(form.EmailAddress.value);
if (!testresult)
{
alert ("Please enter a valid EMail Address (or leave blank)");
form.EmailAddress.focus();
return false;
}



//Either telephone or Email should be provided
if ((form.EmailAddress.value =="") && (form.Telephone.value ==""))
{
alert("Please enter either Telephone or Email Address");
form.EmailAddress.focus();
return false;
}


function test(src) {
var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
return regex.test(src);
}

return true;
}

</SCRIPT>

Thanks in advance

Stuart

Ancora
12-05-2006, 04:13 PM
Stuart:

I have a name, please address me by it.

And PLEASE use the code tags. It's the # on the toolbar.



<form action="ISCpostmail.asp" method="post" name="email" id="email" onSubmit =return checkForm this)>


Unless the above is a copy/paste error, you are missing the first paren around 'this', and the double quotes around it all:

<form action="ISCpostmail.asp" method="post" name="email" id="email" onSubmit ="return checkForm(this)">


You have to change this:


testresult = test(form.EmailAddress.value);


to this:


testresult = testEmail(form.EmailAddress.value);



and this:


function test(src) {
var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
return regex.test(src);
}


to this:


function testEmail(emailStr) {
var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
return regex.test(emailStr);
}

Wing99
12-05-2006, 04:37 PM
Stuart:

I have a name, please address me by it.




Ancora :)

im a mod on another forum (a gaming one not a coding one) and as part of the rules there we are asked to only use personal names in PMs (Unless they have used their name in the post. :) )

im sorry if i coursed offense, kinda mixing rule sets in my head, :o it gets a bit confusing when looking at coding all day ( my brains a bit tired)

Thank you for the advice, ( i just had our old computer guy visit us and he managed to get it to work - although he didnt use the same method as you did - i guess this means im gonna have to have a play after work and try and work out what the difference is between them) :)

Once again

Thankyou for the speedy response

Stuart

Ancora
12-05-2006, 04:58 PM
Stuart:

I think you will find that your "computer guy" did essentially the same as I. The onsubmit part of your opening form tag required the changes I made. And you cannot use "test" as a function name.

Thank you for you explanation about "names," but I do consider it to be rude if not insulting and ungrateful when I'm not addressed by name. Machines aren't posting code here for those in need, people are. To fail or refuse to address someone by name, who expends his valuable time to help you, is to deny that person his dignity.

It's simple common, courtesy.

Take care.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum