Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression for email for a specific domain name

Tags:

c#

regex

asp.net

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.

like image 477
Abhishek Bera Avatar asked Sep 28 '11 09:09

Abhishek Bera


People also ask

How do I find the regex for a domain name?

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.

How do I make an email pattern in regex?

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

Can email be validated with regex?

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.

What is the simplest regular expression for email address?

Regex : ^(.+)@(.+)$ This one is simplest and only cares about '@' symbol. Before and after '@' symbol, there can be any number of characters.


2 Answers

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

should do the trick

like image 98
Muhammad Hasan Khan Avatar answered Oct 25 '22 01:10

Muhammad Hasan Khan


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)$"
like image 25
stema Avatar answered Oct 25 '22 00:10

stema