I have a custom validator that is supposed to prompt the user to removed certain characters if found in the textbox. However, the validation is coming up even when there are no matching characters in the textbox.
I have tested the regex before implementing it in asp.net but it cannot pass the validation.
asp:RegularExpressionValidator ID="revHarmfulCharacters" runat="server"
ErrorMessage="Please remove these characters where present >, <, /*, *\, --, |, {}"
ControlToValidate="txt_comment" ValidationExpression="[/^{}|<>(--)(/*)(*\/)(>=)]"
Display="Dynamic">
</asp:RegularExpressionValidator>
The regular expression should be shown for this case
But should not be shown for this case
This is a test of the regex that I did
Regular expression validation are written to match valid input. Since your regex pattern was not matched entire input string in both cases, so the error message is showed up in both cases.
You could try this pattern: ^[^/{}|<>(--)(/*)(*\/)(>=)^]+$
Edit the regex and test it.
Regex is ValidationExpression="[\^{}|<>(--)(*)(*\/)(>=)]" Try this
I try with this <bibin#212.com>. It is matched.
I try with this bibin#212.com. It is not matched.
I'm new on Asp. I have a problem to using regex for checking password input. Here the regex
<asp:RegularExpressionValidator ID="Regex1" runat="server"
ErrorMessage="Password must contain: Minimum 8 characters atleast 1 UpperCase Alphabet, 1 LowerCase Alphabet, 1 Number and 1 Special Character"
Font-Italic="True" Font-Size="Small" ForeColor="Red"
ValidationExpression="^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$#$!%*?&])[A-Za-z\d$#$!%*?&]{8,}"
ControlToValidate="TextBoxNewPassword" Display="Dynamic" />
When I input "Hamlida123#" regex did'nt allow it. How to solve this?
You need to include the '#` character specifically in the regex, like so:
ValidationExpression="^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$#$!%*?&#])[A-Za-z\d$#$!%*?&#]{8,}"
Demo
Based on your current regex, I assume that you are only allowing certain non-word characters, and so you would need to list every allowable character in your regex as shown above.
I have a text field and I want to have a validation which verify if it starts with 0 the length will be 10, else will be 13.
I want to know if I can write in validation expression like "^0..." not in function.
I hope you can help me.
I code in ASP.NET, C#.
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" Text="dsg" ErrorMessage="RegularExpressionValidator" ValidationExpression="[^0]{0-10},[^1]{0-13}" ControlToValidate="TextBox1"></asp:RegularExpressionValidator>
This doesn't work , but I want something like this.
Your regex [^0]{0-10},[^1]{0-13} means:
[^0] - Match anything but 0 (even a comma)
{0-10}, - Match literal string {0-10}, (because of the hyphen inside {...})
[^1] - Match anything but 1
{0-13} - Match literal {0-13} string.
If you plan to use only server-side validation, you can use .NET regex to validate
if it starts with 0 the length will be 10 , else will be 13.
:
^(0)?(?(1)\d{9}|\d{13})$
See demo
In case you also plan to use it on the client side, you will need a JavaScript regex flavor where you cannot use conditionals:
^(?:0\d{9}|\d{13})$
See another demo
[^0]{0-10} means from 0 up to 10 characters not equal to 0. The ^ does not have the same meaning in the beginning of the regular expression and later.
A regular expression for a string of 10 characters starting with 0 is ^0.{9}$.
The other is ^[^0].{12}$.
So together: ^(0.{9}|[^0].{12})$
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
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.