views:

61

answers:

4

I want to validate the e-mail address entered by the user that it is like that format [email protected]. iti.gov.eg must be writen in the e-mail address. The user must enter his e-mail address in that format in text box.

And how can I retrive it from the text box and check it?

My code is:

var r=/^([a-z\.])+\@(iti)+\.+(gov)+\.+(eg)+$/;
if (!r.test(string))
    alert("the email is not correct");
else 
    alert("your email is correct");

But this is wrong. Can any one help me please?

A: 

Yes it is wrong because [email protected] will be matched. As the + means once or more.

You only need /^[a-z.]+@iti\.gov\.eg$/.

KennyTM
I didn't copy your answer, just been tooooo slow :)
dwo
@dwo: it always happens in simple questions like this :)
KennyTM
A: 

Be careful with the dots in a regex: you write .+ which means 1 to n random characters. I think you meant:

/^([a-zA-Z0-9]+)@iti\.gov\.eg$/
dwo
A: 
 /^[a-z][\w.+-]+@iti\.gov\.eg$/

This regex ensures that the name part starts with a letter. ., +, - etc are valid characters in an email.

And yeah, email validation is a tricky thing.

Amarghosh
+3  A: 

See this question on how to validate an email using regular expressions in JavaScript:

Validate email address in Javascript?

Once you know that it is a valid email address you can then check to make sure that it contains the string @iti.gov.eg (case insensitive) which is a much easier task.

Kragen