...

View Full Version : email validation help



cappincrappy
08-31-2004, 06:52 AM
I am definitly a noob when it comes to js, but I was hoping that someone could look past that and suggest some advice to help complete this "simple" email validation.

Here's what I have so far:

if (document.myForm.email.value != email)
{
alert("Please provide a valid email address.")
return false;
}

So I know what I need to do and that is create a variable called "email" that is equal to what an email address should have i.e. some text followed by an "@" symbol, followed by some text, followed by a ".", and finally followed by "com, net, org, gov, biz etc.

I have no idea how to write this variable.

I understand there are more thorough ways of doing this but this is really all I need.

Any help would be greatly appreciated :)

Philip M
08-31-2004, 08:16 AM
How strange that no-one has ever asked this queston before!

http://www.javascriptkit.com/script/script2/acheck.shtml

or take the trouble to use the search feature of this forum.

jamescover
08-31-2004, 08:53 AM
No doubt, someone will suggest regular expressions, but unless you want to take the time to learn them, you will always be dependent on someone else to fix your code, if the error checking criteria changes.

At any rate, below is a simple script, using variables, similar to what I use:



<script>
<!--

function chkFrm(){

var doc = document['oFrm']['email'];
var badChar = " /\\+()*&^%$#?<>=!~`[]|:;,'";

if (doc.value == ""){
alert("missing email...!");
return false;
}

for (j=0;j<badChar.length;j++){
var illChar = badChar.charAt(j);

if (doc.value.indexOf(illChar,0) != -1){
alert("email contains illegal character(s)...!");
return false;
}

var atPos = doc.value.indexOf("@",1);

if (atPos == -1){
alert("missing, or illegal position of, \n \@ sign in email...!");
return false;
}

var dotPos = doc.value.indexOf(".",3);

if (dotPos == -1 || dotPos < atPos || dotPos < atPos+2){
alert("missing, or illegal position of, \n dot (.) in email...!");
return false;
} else {
document['oFrm'].submit();
}
}
}

//-->
</script>

<form name="oFrm" method="get" action="somefile.php">
<input name="email" type="text" />
<input name="oBtn" type="button" value="submit" onclick="javascript:chkFrm();" />
</form>

This will check:

1). If the email is empty.
2). If the email contains any of the illegal characters listed.
3). If the email contains an @ sign.
4). If the @ sign is the first character.
5). If the email contains a dot.
6). If the dot comes before the @, or is the 1st character after the @.

It's not comprehensive, but will pick up most common errors.


-james

trib4lmaniac
08-31-2004, 10:41 AM
If it helps the regex for an email is
address.match(/^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z0-9._%-]{2,4}$/i);I think I've got the syntax right for javascript :rolleyes:

fci
08-31-2004, 11:44 AM
If it helps the regex for an email is
address.match(/^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z0-9._%-]{2,4}$/i);I think I've got the syntax right for javascript :rolleyes:

I don't think underscores ever come after an @ symbol.... and.. percent symbols?
and I prefer patteren.test(str) since it would only return true/false:
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/regexp.html#1194128

trib4lmaniac
08-31-2004, 11:57 AM
Come to think of it, I don't think you can have an underscore in the domain at all! Percentages for things like %20 (space) etc.

fci
08-31-2004, 12:40 PM
Come to think of it, I don't think you can have an underscore in the domain at all! Percentages for things like %20 (space) etc.

...
I didn't know spaces were allowed in an email address...... ?

trib4lmaniac
09-01-2004, 10:11 AM
Not spaces, it's entity. Which is %20.
If you're going to be picky then I think almost any character can come before the @ symbol as long as it is between quotation marks. (ie. "my&very$long~and#legal (i think) email!"@address.com)
Might not be though :rolleyes:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum