开发者

regular expression to find email addresses from specific domain

开发者 https://www.devze.com 2022-12-18 21:39 出处:网络
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-m

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

精彩评论

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