Regular Expression for email for a specific domain name - c#

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$
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

Related

Restrict all the special characters through a regex

There is a long set of characters that are not allowed to validate an input box of winform app.
So i figured that rather than making the long list that are not allowed make the shorter one that are allowed.
The set that is allowed are (a-z,A-Z, 0-9,#,.) .Rest every thing that can be entered are not allowed.
This is the regex that i have made for this.
Regex.IsMatch(textBox1.Text, #"[#\.\w]+$")
It seem to work in some cases but when i enter the data in this format normal character or number special character normal character or number it seems to break few example ee(vv, 55)44,aba&3B.
Not able to figure out whats wrong in this.
Your regex is not valid, because you don't validate all string, but the last part.
You should start it with ^ - beginning of the line symbol.
Regex.IsMatch(textBox1.Text, #"^[\w#.]*$")
\w also means letters in every language, so it will validate the string "абц" too.
So if you need only for english, use
Regex.IsMatch(textBox1.Text, #"^[a-zA-Z0-9#.]*$")
Try this :
Regex.IsMatch(textBox1.Text, #"^[a-zA-Z0-9#.]*$")
Use
^[-a-zA-Z0-9 _ - \. #]*
as the Regex expression text.

how to detect input that is not in english?

I'm trying to detect if input is not in English chars, will disallow the input and i'm using code below to validate the input. The code works fine if the input is in Non-English, for example, 'ກັ​ຫ​ກ່​ຫ່', '你好'. When the input contains English chars and Non-English chars, the code below will allow the input to go through and i don't want this to happen. How can i disallow the input if there is any Non-English chars detected in the input?
If Not Regex.IsMatch(Edt.Text, "[A-Za-z0-9]") Then
End If
Use this regex:
^[A-Za-z0-9]*$
That Regex.IsMatch call will succeed if Edt.Text contains any Latin letter or Arabic digit.
First, you'll need to define the problem more clearly; in particular, you'll need to decide exactly which characters are permitted (think about spaces and punctuation).
Then you'll need to modify the regular expression so it matches the entire string, probably something like:
"^[something]*$"
where something is left as an exercise.

Pattern matching for swedish character

I need a help regarding regular expression.
I have to match string like this:
âãa34dc
Pattern that i have used:
\s*[a-zA-Z]+[a-zA-Z_0-9]*\s
but this pattern is not good enough to identify this kind of string e.g. âãa34dc
P.S. âã these are swedish character.
Please help me for find out correct pattern for this kind of string.
Do you actually want to restrict it to Swedish characters? In other words, should a German character not match? If so, then you'll probably have to enumerate the whole alphabet, and include that.
If what you really want is to match every alphabetic character, use the regular expression terms for matching all letters.
\w matches any word character, but that includes numbers & some punctuation. That's close, but not exactly what you want for your second term.
For the first term, where you don't want to include numbers, specifying that the character should be a Unicode 'letter' class will work. \p{L} specifies all Unicode characters that are a letter. This includes [a-zA-Z], and all the Swedish characters, and German, and Russian, etc.
Therefore, I think this regular expression is what you want:
\s*[\p{L}][\p{L}_0-9]*\s
If you want to include digits from other character sets, and some other punctuation, then you can use [\w]* for the second term.
please give a set of rules.
according to your question :
[X-Ya-zA-Z]{3}[0-9]{2}[a-zA-Z]{2}
Replace X with the first swedish letter
Replace Y with the last swedish letter
John Machin provides a great answer for this. Adapting his pattern, what you need is probably something similar to: \s*[^\W\d_]\w*\s*
P.S. I removed the + quantifier from your first part. Any subsequent letters would be matched by the subsequent quantified \w.

Password Regex (client side javascript)

I need a regex for the following criteria:
Atleast 7 alphanumeric characters with 1 special character
I used this:
^.*(?=.{7,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[##$!%^&+=]).*$
It works fine if I type Password1! but doesnt work for PASSWORD1!.
Wont work for: Stmaryshsp1tal!
I am using the Jquery validation plugin where I specify the regex.
When I use a regular expression validator and specify the following regex:
^.*(?=.{7,})(?=(.*\W){1,}).*$
It works perfectly without any issues. When I set this regex in the Jquery validation I am using it doesnt work.
Please can someone shed some light on this? I want to understand why my first regex doesnt work.
(?=.\d)(?=.[a-z])
tries to match a digit and an alphanumeric character at the same place. Remember that (?= ... ) does not glob anything.
What you want is probably:
^(?=.*\W)(?=(.*\w){7})
This is exactly the same as veryfying that your string both matches ^.*\W (at least one special character) and ^(.*\w){7}) (7 alphanumeric characters. Note that it also matches if there are more.
Try this regex:
\S*[##$!%^&+=]+\S*(?<=\S{7,})
EDIT3: Ok, this is last edit ;).
This will match also other special characters. So if you wan't limit the number of valid characters change \S to range of all valid characters.
Here is the regex , I think it can handle all possible combination..
^(?=.{7,})\w*[.##$!%^&+=]+(\w*[.##$!%^&+=]*)*$
here is the link for this regex, http://regexr.com?2tuh5
As a good tool for quickly testing regular expressions I'd suggest http://regexpal.com/ (no relations ;) ). Sometimes simplifying your expression helps a lot.
Then you might want to try something like ^[a-zA-Z0-9##$!%^&+=]{7,}$
Update 2 now including digits
^.*(?=.{7,})(?=.*\d)(?=.*[a-zA-Z])(?=.*[##$%^&+=!]).*$
This matches:
Stmarysh3sptal!, password1!, PASSWORD1P!!!!!!##^^ASSWORD1, 122ss121a212!!
... but not:
Password1, PASSWORD1PASSWORD1, PASSWORD!, Password!, 1221121212!! etc
The reason it matches Password1! but not PASSWORD1! is this clause:
(?=.*[a-z])
That requires at least one lowercase letter in the password. The pattern says that the password must be at least 7 characters long, and contain both uppercase and lowercase letters, at least one number, and at least one of ##$!%^&+=. PASSWORD1! fails because there are no lowercase letters in it.
The second pattern accepts PASSWORD1! because it's a far, far weaker password requirement. All it requires is that the password is 7+ characters and has at least one special character in it (other than _). The {1,} is unnecessary, by the way.
If I were you, I'd avoid weakening the password and just leave it as it is. If I wanted to allow all-lowercase or all-uppercase passwords for some reason, I'd simply change it to
^(?=.*\d)(?=.*[a-zA-Z])(?=.*[##$!%^&+=]).{7,}$
...thus not weakening the password requirements any more than I had to.

How to match a comma separated list of emails with regex?

Trying to validate a comma-separated email list in the textbox with asp:RegularExpressionValidator, see below:
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
runat="server" ErrorMessage="Wrong email format (separate multiple email by comma [,])" ControlToValidate="txtEscalationEmail"
Display="Dynamic" ValidationExpression="([\w+-.%]+#[\w-.]+\.[A-Za-z]{2,4},?)" ValidationGroup="vgEscalation"></asp:RegularExpressionValidator>
It works just fine when I test it at http://regexhero.net/tester/, but it doesn't work on my page.
Here's my sample input:
test#test.com,test1#test.com
I've tried a suggestion in this post, but couldn't get it to work.
p.s. I don't want a discussion on proper email validation
This Regex will allow emails with spaces after the commas.
^[\W]*([\w+\-.%]+#[\w\-.]+\.[A-Za-z]{2,4}[\W]*,{1}[\W]*)*([\w+\-.%]+#[\w\-.]+\.[A-Za-z]{2,4})[\W]*$
Playing around with this, a colleague came up with this RegEx that's more accurate. The above answer seems to let through an email address list where the first element is not an email address. Here's the update which also allows spaces after the commas.
Try this:
^([\w+-.%]+#[\w-.]+\.[A-Za-z]{2,4},?)+$
Adding the + after the parentheses means that the preceding group can be present 1 or more times.
Adding the ^ and $ means that anything between the start of the string and the start of the match (or the end of the match and the end of the string) causes the validation to fail.
The first answer which is selected as best matches the string like abc#xyz.comxyz#abc.com which is invalid.
The following regex will work for comma separated email ids awesomely.
^([\w+-.%]+#[\w.-]+\.[A-Za-z]{2,4})(,[\w+-.%]+#[\w.-]+\.[A-Za-z]{2,4})*$
It will match single emailId, comma separated emailId but not if comma is missed.
First group will match string of single emailId. Second group is optionally required by '*' token i.e. either 0 or more number of such group but ',' is required to be at the beginning of such emailId which makes comma separated emailId to match to the above regex.
A simple modification of #Donut's answer allows adjacent commas, all TLDs of two characters or more, and arbitrary whitespace between email addresses and commas.
^([\w+-.%]+#[\w-.]+\.[A-Za-z]{2,}(\s*,?\s*)*)+$
You will need to split and remove whitespace and empty strings on your side, but this should be an overall better user experience.
Examples of matched lists:
person#example.co,chris#o.com,simon#example.capetown
person#example.co ,, chris#o.com, simon#example.capetown
^([a-zA-Z0-9_.+-]+#[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+,*[\W]*)+$
This will also work. It's a little bit stricter on emails, and doesn't that there be more than one email address entered or that a comma be present at all.
The following RegEx will work even with some of the weirdest emails out there, and it supports a comma between emails.
((?:[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")#(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-zA-Z0-9-]*[a-zA-Z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\]),?)+
A few Examples:
Valid: planetearth#solar.com
Valid: planet.earth#solar.com
Valid: planet.earth#solar.com,blue.planet#solar.com
Valid: planet-earth#solar-system.com,/#!$%&'*+-/=?^_`{}|~#solar.org,"!#$%&'-/=^_`{}|~.a"#solar.org
Invalid: planet earth#solar.com
Hope This helps.
^([\w+.%-]+#[\w.-]+\.[A-Za-z]{2,})( *,+ *(?1))*( *,* *)$
The point about requiring a comma between groups, but not necessarily at the end is handled here - I'm mostly adding this as it includes a nice subgroup with the (?1) so you only define the actual email address regex once, and then can muck about with delimiters.
Email address ref here: https://www.regular-expressions.info/email.html
The regex below is less restrictive and more appropriate for validating a manually-entered list of comma-separated email addresses. It allows for adjacent commas.
^([\w+-.%]+#[\w-.]+\.[A-Za-z]{2,4},*[\W]*)+$
Use the following regex, it will resolve your problem. The following regex will entertain post and pre spaces with comma too
/^((([a-zA-Z0-9_-.]+)#([a-zA-Z0-9_-.]+).([a-zA-Z\s?]{2,5}){1,25})(\s?,\s*?))$/
I'm a bit late to the party, I know, but I figured I'd add my two cents, since the accepted answer has the problem of matching email addresses next to each other without a comma.
My proposed regex is this:
^[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,}(,[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,})*$
It's similar to the accepted answer, but solves the problem I was talking about. The solution I came up with was instead of searching for "an email address followed by an optional comma" one or more times, which is what the accepted answer does, this regex searches for "an email address followed by an optional comma prefixed email address any number of times".
That solves the problem by grouping the comma with the email address after it, and making the entire group optional, instead of just the comma.
Notes:
This regex is meant to be used with the insensitive flag enabled.
You can use whichever regex to match an email address you please, I just used the one that I was already using. You would just replace each [A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,} with whichever regex you want to use.
The solution that work for me is the following
^([a-zA-Z0-9_.+-]+#[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)(,([a-zA-Z0-9_.+-]+#[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+))*
The easiest solution would be as following. This will match the string with comma-separated list. Use the following regex in your code.
Regex: '[^,]+,?'
^([\w+-.%]+#[\w-.]+\.[A-Za-z]+)(, ?[\w+-.%]+#[\w-.]+\.[A-Za-z]+)*$
Works correctly with 0 or 1 spaces after each comma and also for long domain extensions
This works for me in JS and TS
^([a-z0-9!#$%&'*+/=?^_`{|}~.-]+#[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)*)(([, ]+[a-z0-9!#$%&'*+/=?^_`{|}~.-]+#[a-z0-9]([a-z0-9-]*[a-z0-9])\.([a-z0-9]([a-z0-9-]*[a-z0-9]))*)?)*$
You can check it out here
https://regex101.com/r/h0l9ks/1
The regex i have for this issue all well except that we need to add comma after every email address.
^((\s*?)[a-zA-Z0-9._%-]+#[a-zA-Z0-9.-]+\.[a-zA-Z,]{2,4}(\s*?),)*
The explanation for this will be like this:
(\s*?) will allow spaces at the start.
[a-zA-Z0-9._%-]+#[a-zA-Z0-9.-]+\.[a-zA-Z,]{2,4} is common email pattern.
(\s*?) will allow space at the end too.
, will restrict comma.
For me, this one works perfectly for multiple emails:
^(\w+((-\w+)|(\.\w+))*\#[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]{2,4}\s*?,?\s*?)+$
RegEx Component
Explanation
^
Matches the start of the string.
\w+
Matches one or more word characters (letters, digits or underscores).
((-\w+)|(\.\w+))*
Matches zero or more occurrences of a hyphen followed by one or more word characters or a period followed by one or more word characters.
\#
Matches the # symbol.
[A-Za-z0-9]+
Matches one or more letters or digits.
((\.|-)[A-Za-z0-9]+)*
Matches zero or more occurrences of a period or hyphen followed by one or more letters or digits.
\.[A-Za-z0-9]{2,4}
Matches a period followed by two to four letters or digits.
\s*?,?\s*?
Matches optional whitespace followed by an optional comma followed by optional whitespace.
+
Matches one or more occurrences of the entire expression.
$
Matches the end of the string.

Categories

Resources