开发者

regular expression to find email addresses from specific domain

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?


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.


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$/.


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$/


 /^[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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜