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.
The domain name should be a-z or A-Z or 0-9 and hyphen (-). The domain name should be between 1 and 63 characters long. The domain name should not start or end with a hyphen(-) (e.g. -geeksforgeeks.org or geeksforgeeks.org-). The last TLD (Top level domain) must be at least two characters and a maximum of 6 characters.
[a-zA-Z0-9+_. -] matches one character from the English alphabet (both cases), digits, “+”, “_”, “.” and, “-” before the @ symbol. + indicates the repetition of the above-mentioned set of characters one or more times. @ matches itself.
Regex provides the ability to validate the structure of an email address. It can be handled with one or two lines of code and can easily be tweaked to handle a wide variation of different parameters. However, one thing to keep in mind is that it can only check the structure of an email address.
Regex : ^(.+)@(.+)$ This one is simplest and only cares about '@' symbol. Before and after '@' symbol, there can be any number of characters.
^[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)$"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With