I currently have the following validation expression for one of my asp.net controls, which ensures the user has entered, what we consider to be a valid UK postcode:
ValidationExpression="^\s*([A-Z]{1,2}[0-9R][0-9A-Z]?\s*[0-9][ABD-HJLNP-UW-Z]{2})\s*$"
This works fine if the user enters their postcode using uppercase, but I'd like it to ignore case and am not sure how to incorporate that into the above expression?
I'd like it to ignore case
Activate the ignore case flag by adding this notation to your regex: i.
Your regex would like this one below:
ValidationExpression="/^\s*([A-Z]{1,2}[0-9R][0-9A-Z]?\s*[0-9][ABD-HJLNP-UW-Z]{2})\s*$/i"
The only simple solution is to put lowercase letters everywhere, i.e.: [0-9A-Za-z]
Other solutions are not always reliable.
Related
I want to restrict a text box to accept only two strings i.e true/false using regular expression.
My code is working partially. For lower case, it's working fine but I want it to be case insensitive.
my regular expression looks like this
<asp:RegularExpressionValidator ID="regAssign1" runat="server"
ControlToValidate="tb_Assign" ErrorMessage="Wrong Input!" forecolor="Red"
ValidationExpression="^(true)|(false)$" ValidationGroup="Submit"></asp:RegularExpressionValidator>
I want to get this done using a regular expression validator only. Suggestions using any other options like use JavaScript or use a drop-down list should be avoided.
You should change your regex to:
"^(true|false)$"
Please note that when using that, editor still accept empty string so you need to add a RequiredFieldValidator as well. Also for both of them if you added :
Display="Dynamic"
it would look better.
The simple way is just like this...
^true$|^false$
This version allows whitespace before or after, which you can remove later with Trim.
^\s*true\s*$|^\s*false\s*$
I persistently tried to use ?i: for case insensitivity, it did not work in my RegularExpressionValidator. So here are some possible alternatives that will allow the three most likely case versions "true", "True", and "TRUE".
^true$|^false$|^True$|^False$|^TRUE$|^FALSE$
and allowing whitespace...
^\s*true\s*$|^\s*false\s*$|^\s*True\s*$|^\s*False\s*$|^\s*TRUE\s*$|^\s*FALSE\s*$
Also, as another answer pointed out, you will need a RequiredFieldValidator to disallow an empty TextBox.
Try this regex:
^(?i:true)|(?i:false)$
The i makes comparison case-insensitive for the words following the colon.
Alternatively, try this:
^([T|t][R|r][U|u][E|e])|([F|f][A|a][L|l][S|s][E|e])$
What is the default regular expression used by asp.net create user wizard?
According to the MSDN documentation, it should be something like this:
Regular Expression: #\"(?:.{7,})(?=(.*\d){1,})(?=(.*\W){1,})
Validation Message: Your password must be 7 characters long, and contain at least one number and one special character.
However, it does not work as it does not accept something like 3edc£edc, which is actually accepted when using the default create user wizard.
Any idea about how can I get this regular expression?
The error is in the ?: in (?:.{7,})(?=(.*\d){1,})(?=(.*\W){1,}) that is "consuming" the fist seven characters or more characters. It should be ?= OR you can invert the order: (?=(.*\d){1,})(?=(.*\W){1,})(?:.{7,})
Just change the order
^(?=(.*\d))(?=(.*\W)).{7,}
I additionally removed the {1,} and anchored it to the start of the string and you don't need a group around the last part
See it here on Regexr
I need a regex for the ASP.Net (4) Regex Validation control. It needs to be a RegEx validator to support other dynamic behaviors outside the scope of this post..
I was using the following, but it fails if the user enters the % sign following the number (which is a req of my spec):
^(100(?:\.0{1,2})?|0*?\.\d{1,2}|\d{1,2}(?:\.\d{1,2})?)$
I tried adding an atomic group of ^(?>%?) at the end, with no luck, after reading the excellent post
Regular expression greedy match not working as expected
Does anyone have any ideas?
Try this
^(100(?:.0{1,2})?%?|0*?.\d{1,2}%?|\d{1,2}(?:.\d{1,2})?%?)$
try this one instead:
^0*(100(\.00?)?|[0-9]?[0-9](\.[0-9][0-9]?)?)%?$
Looks like a simple task - get a regex that tests a string for particular length:
^.{1,500}$
But if a string has "\r\n" than the above match always fails!
How should the correct regex look like to accept new line characters as part of the string?
I have a <asp:TextBox TextMode="Multiline"> and use a RegularExpressionValidator to check the length of what user types in.
Thank you,
Andrey
You could use the RegexOptions.Singleline option when validating input. This treats the input as a single line statement, and parses it as such.
Otherwise you could give the following expression a try:
^(.|\s){1,500}$
This should work in multiline inputs.
Can you strip the line breaks before checking the length of the string? That'd be easy to do when validating server-side. (In .net you could use a custom validator for that)
From a UX perspective, though, I'd implement a client-side 'character counter' as well. There's plenty to be found. jQuery has a few options. Then you can implement the custom validator to only run server-side, and then use the character counter as your client-side validation. Much nicer for the user to see how many characters they have left WHILE they are typing.
The inability to set the RegexOptions is screwing you up here. Since this is in a RegularExpressionValidator, you could try setting the options in the regular expression itself.
I think this should work:
(?s)^.{1,500}$
The (?s) part turns on the Singleline option which will allow the dot to match every character including line feeds. For what it's worth, the article here also lists the other RegexOptions and the notation needed to set them as an inline statement.
I'm using an asp.net Web Forms RegularExpressionValidator Control to validate a text field to ensure it contains a series of email addresses separated by semicolons.
What is the proper regex for this task?
I think this one will work:
^([A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}(;|$))+
Breakdown:
[A-Za-z0-9._%+-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4} : valid email (from http://www.regular-expressions.info/)
(;|$) : either semicolon or end of string
(...)+ : repeat all one or more times
Make sure you are using case-insensitive matching. Also, this pattern does not allow whitespace between emails or at the start or end of the string.
The 'proper' (aka RFC2822) regex is too complicated. Try something like (\S+#[a-zA-Z0-9-.]+(\s*;\s*|\s*\Z))+
Not perfect but should be there 90% (haven't tried it, so it might need some alteration)
Note: Not too sure about \Z it might be a Perl only thing. Try $ as well if it doesn't work.