-1

I want to validate the e-mail address entered by the user that it is like that format anything@iti.gov.eg. 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?

Brad Mace
  • 27,194
  • 17
  • 102
  • 148
sama
  • 1
  • 1
  • 2
  • Why not have `\.iti\.gov\.eg`? – Skilldrick Jan 28 '10 at 11:00
  • 2
    If your user's emails must end with "@iti.gov.eg" - why not simply ask them to enter only their user name part? Then simply append "@iti.gov.eg" to what ever they type. It will save user's the trouble of typing and you the trouble of complex regex. – logout Jan 28 '10 at 11:13

4 Answers4

3

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.

Community
  • 1
  • 1
Justin
  • 84,773
  • 49
  • 224
  • 367
0

Yes it is wrong because anything@itiiti.govgov.egeg will be matched. As the + means once or more.

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

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
0

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
  • 3,576
  • 2
  • 22
  • 39
0
 /^[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.

Community
  • 1
  • 1
Amarghosh
  • 58,710
  • 11
  • 92
  • 121