Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validate E-mail address

Status
Not open for further replies.

BobbaFet

Programmer
Feb 25, 2001
903
NL
Hello all,

I'm a programmer who's been assigned to write a procedure that checks if an email address could be correct (as it doesn't actually have to exist, but it must at least be made up correctly) and am trying to find out what characters you can and cannot use in an email address.

I came across this function in this forum:

Code:
function checkemail(){
var str=document.validation.emailcheck.value
var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
if (filter.test(str))
testresults=true
else{
alert("Please input a valid email address!")
testresults=false
}
return (testresults)
}
</script>

I assume that the var called filter checks for characters that cannot be in the email address, but I do not understand it. Can anyone please explain this to me?

Should you need it, the rest of the post can be found here:

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
[/b]
Great Delphi Websites faq102-5352
 

The filter string you are seeing is a regular expression, designed to be a set of rules to check the email address against.

RegExp is not one of my strong points, so I can't tell you exactly what it does, but probably things like checking for the number of @ signs, checking for certain number of characters betweeen . characters, etc, are all fairly routine things for one of these.

Hope this helps,
Dan

 
Nor mine, I program in Delphi and this looks nothing like Delphi code so I can't make up what it all means unfortunately.

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
[/b]
Great Delphi Websites faq102-5352
 
FYI, you might get a better answer in the Javascript forum:

forum216

--James
 
I would've posted it there, but I found the thread containing that script on this forum

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
[/b]
Great Delphi Websites faq102-5352
 
Good starter on javascript regular expressions:


Good reference:


As for:

Code:
/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i

// encapsulates the regular expression.
\w means any letter, number or underscore.
+ means one or more of (preceding thing).
(?:....) is a grouping thing.
/i means not case-sensitive.

So, very loosely, this expression is saying:

Match any letter, number or underscore (or '-') in any amount of groups with '.' marking the groups. This is followed by a '@'. This is followed again by any letter, number or underscore (or '-') in any amount of groups with '.' marking the groups. This is followed by a '.' with groups of a-z characters (2-6 char long) again grouped by '.'. The whole thing is not case sensitive.

Possibly.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top