简体   繁体   中英

Regular Expression for email for a specific domain name

This is my code:

<asp:RegularExpressionValidator ID="regexEmail" 
     runat="server" ValidationExpression="(\w+@[test]+?\.[com]{3})" 
     ErrorMessage="Please Enter Valid Email Id" 
     ControlToValidate="txtEmailAddress" />

which is not working. But my problem does not end there. I would also like to know how to validate only characters in the username, and not numbers and special characters.

Please if anybody could help with this code. Thanks in Advance.

^[a-zA-Z]+@yourdomain\.com$

应该做的伎俩

At first I think there is a miss understanding of the square brackets. With [com]{3} you are createing a character class and match 3 characters out of this class, that means this will match com (I think as you wanted), but also ccc , cmc and so on.

Similar for [test]+? , this matches at least 1 character from t , e and s

When you say:

validate only characters in the username, and not numbers and special characters

I think you mean only letters? Or only ASCII letters?

What you meant is probably

(\w+@test\.com)

\\w is a character class that contains A-Za-z0-9 and _ (and maybe anything thats a letter in unicode, I am not sure). If you want only ASCII characters then create your own character class with [A-Za-z] , if you want to allow any letter use the Unicode property \\p{L}

Something like this

@"^(\p{L}+@test\.com)$"

[test] is probably not what you want. It's equivalent to [tes] and means 't' or 'e' or 's'.

Did you try something like:

^[a-zA-Z]+@.*\.com$

This will validate to emails of the form xxx@xxx.com

As C# literal this is "^[a-zA-Z]+@.*\\\\.com$" .

MSDN has an article about email validation.

http://msdn.microsoft.com/en-us/library/01escwtf.aspx

cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM