Regular Expression for TimeSpan in format "hh.mm" - asp.net

I want to create RegularExpressionValidator for validation TextBox format hh.mm.
This expression works:
^([0-9]|0[0-9]|1[0-9]|2[0-3]).[0-5][0-9]$
But if I insert 5454 in the TextBox it also passes, but it shouldn't.

. is a meta character in regular expressions that matches any character. If you want to match literally just a period, then you need to escape it:
^([0-9]|0[0-9]|1[0-9]|2[0-3])\.[0-5][0-9]$

you forgot to escape .
try
^([0-9]|0[0-9]|1[0-9]|2[0-3])\.[0-5][0-9]$

Related

RegEx Max and Min length characters for a Text Box

I use Asp.net and C#.
I need force the User to add in a TextBox Control only between 4 and 128 characters text.
I would like to use a ValidationExpression Property for a Validation Control.
Could you point me out a correct Regular Expression?
Notes: I'm using this code right now, but it seems not working properly if there are double spaces or break line in the TextBox
ValidationExpression="^.{4,128}$"
Thanks for your time on this!
Your expression is correct. Just use the Singleline modifier, to make the dot also match newline characters.
RegexOptions.Singleline
Or as inline modifier
"^(?s)(.){4,128}$"
RegexOptions Enumeration
Regular Expression Options
The full stop or period character (.) is known as dot. It is a wildcard that will match any character except a new line (\n).
Reference: http://www.radsoftware.com.au/articles/regexlearnsyntax.aspx
Try this instead:
ValidationExpression = "^(.|\n|\t){4,128}$"
I added tabs (\t) as well.
Tell me if it worked or not!
try this ValidationExpression = ^(\w*)(\s*)(.*){4,128}$" it will cover periods and spaces as well.

# in Regular Expression

I created the register form in asp.net. And I want to validate Name. This is not included special characters especially '#' character. I used a regular expression validator. I wrote in ValidationExpression that is ^[A-Za-z0-9.'-_\s]+$. It is OK special characters exact '#' character. How to correct regexp. Please help me.
'-_ means every character between ' and _, which includes a large number of characters.
You should escape the - by writing \-.

Date Regular Expression Validator

I have a regular expression validator on a text box to validate that the text entered is a valid date.
See reg ex below:
ValidationExpression="^[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}$"
Now I want to allow the following in the textbox: mm/dd/yyyy
How can I update my regex so that if mm/dd/yyyy is entered it does not throw a validation error?
Thanks in advance.
ValidationExpression="^[0-9m]{1,2}/[0-9d]{1,2}/[0-9y]{4}$"
Basically allows 0-9 or m in the first field, 0-9 or d in the second, 0-9 or y in the third (in regular expression [] brackets contain a list of possible options, - denote ranges of values when placed within brackets).
This is a more accurate way to restrict the Date to a more meaningful format
^[1-12]{1,2}/[1-31]{1,2}/[2000-2050,1900-1999]{4}$
This is still not perfect as this will allow - for instance - a date 02/31/2013.
Just realized this is quiet buggy.

Regular Expression to test for the number of any characters in a range

I'm using the ASP Validation control a wanted to test that multiline textbox content was between 2 and 40 characters. Any character can be provided, including the new line character. What would be the expression? What I have below fails.
<asp:RegularExpressionValidator ID="RegularExpressionValidator"
runat="server"
ValidationExpression="^[.]{2,40}$"
ControlToValidate="CommentsTextBox"
ErrorMessage="The Comment field must be between 2 and 40 characters"
Display="Dynamic">*</asp:RegularExpressionValidator>
When you put the dot inside square brackets, it loses its special meaning and just matches a literal dot. The easiest way to match any character in an ASP RegularExpressionValidator is like this:
^[\s\S]{2,40}$
[\s\S] is the standard JavaScript idiom for matching anything including newlines (because JS doesn't support DOTALL or s-mode, which allows the dot to match newlines). It works the same in .NET, so you don't have to worry about whether the dot matches newlines, or whether the regex will be applied on the server or the client.
You're treating the period as a literal inside the brackets. You just want:
^(.|\n){2,40}$

Regular expression not working after debugging

I have an ASP.NET website with a regular expression validator text box.
I have changed the expression in the regular expression validation property "validator expression" and after compiling (rebuild) and running, the validation CHANGEs are not reflecting.
The previous validation is working fine but the changed validation is not working.
Please help me!
edit:
First code:
([a-zA-Z0-9_-.]+)\#((base.co.uk)|(base.com)|(group.com))
Second code:
#"([a-zA-Z0-9_\-.]+)#((base\.co\.uk)|(base\.com)|(group\.com)|(arg\.co\.uk)|(arggroup\.com))"
Assuming your "first code" is a literal regex, you need to escape the hyphen in the character class with a backslash.
Your "second code" is a regex formatted as a C# verbatim string that will match an email address such as whatever#base.co.uk just fine. There is nothing wrong with this regex.
You'll have to post the code in which you are using this regex if it doesn't work the way you want.
In this part: [a-zA-Z0-9_\-.] you are escaping the hyphen. The proper way to put a hyphen in a regex character class is in first position (or it thinks it is part of a range):
[-a-zA-Z0-9_.]
Then you removed the backslash from before the #. In Perl the # would be taken as part of a list name, but in C# I am not sure what effect it would have to not escape it.
The escaping of the periods is also suspect. You might need to double them up: e.g. \\. Instead, what I would do for a period is use a character class: i.e. [.] Inside the character class the period loses its special meaning.
Try this:
#"([-a-zA-Z0-9_.]+)\#((base[.]co[.]uk)|(base[.]com)|(group[.]com)|(arg[.]co[.]uk)|(arggroup[.]com))"

Resources