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"
ControlToVa开发者_如何学JAVAlidate="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$
should do the trick
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
精彩评论