Regular expression not working after debugging - asp.net

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))"

Related

RegEx for Client-Side Validation of FileUpload

I'm trying to create a RegEx Validator that checks the file extension in the FileUpload input against a list of allowed extensions (which are user specified). The following is as far as I have got, but I'm struggling with the syntax of the backward slash (\) that appears in the file path. Obviously the below is incorrect because it just escapes the (]) which causes an error. I would be really grateful for any help here. There seems to be a lot of examples out there, but none seem to work when I try them.
[a-zA-Z_-s0-9:\]+(.pdf|.PDF)$
To include a backslash in a character class, you need to use a specific escape sequence (\b):
[a-zA-Z_\s0-9:\b]+(\.pdf|\.PDF)$
Note that this might be a bit confusing, because outside of character classes, \b represents a word boundary. I also assumed, that -s was a typo and should have represented a white space. (otherwise it shouldn't compile, I think)
EDIT: You also need to escape the dots. Otherwise they will be meta character for any character but line breaks.
another EDIT: If you actually DO want to allow hyphens in filenames, you need to put the hyphen at the end of the character class. Like this:
[a-zA-Z_\s0-9:\b-]+(\.pdf|\.PDF)$
You probably want to use something like
[a-zA-Z_0-9\s:\\-]+\.[pP][dD][fF]$
which is same as
[\w\s:\\-]+\.[pP][dD][fF]$
because \w = [a-zA-Z0-9_]
Be sure character - to put as very first or very last item in the [...] list, otherwise it has special meaning for range or characters, such as a-z.
Also \ character has to be escaped by another slash, even inside of [...].

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.

How to escape a RegEx that generates an error of “unexpected quantifier"on IE?

I use asp.net and C#. I have TextBox with an Validation Control with RegEx.
I use this code as validation.
ValidationExpression="^(?s)(.){4,128}$"
But only in IE9 I receive an error: unexpected quantifier from the javascript section.
Probably I have to escape my RegEx but I do not have any idea how to do it and what to escape.
Could you help me with a sample of code? Thanks
Write it like this instead :
^([\s\S]){4,128}$
I suspect that (?s) is the cause of the error.
Three problems: (1) JavaScript doesn't support inline modifiers like (?s), (2) there's no other way to pass modifiers in an ASP validator, and (3) neither of those facts matters, because JavaScript doesn't support single-line mode. Most people use [\s\S] to match anything-including-newlines in JavaScript regexes.
EDIT: Here's how it would look in your case:
ValidationExpression="^[\s\S]{4,128}$"
[\s\S] is a character class that matches any whitespace character (\s) or any character that's not a whitespace character--in other words, any character. The dot (.) metacharacter matches any character except a newline. Most regex flavors (like .NET's) support a "Singleline" or "DOTALL" mode that makes the dot match newlines, too, but not JavaScript.
JavaScript doesn't understand (?s) afaik, instead you can replace . with [^] or [\s\S].
Eg: ^[^]{4,128}$

Numeric textbox with regex

im trying to do a numeric textbox in asp.net using regex, and came up with:
^[^\s]+[/d]+[^\s]$
I want it to disallow leading/trailing whitespace, and allow only numbers.
Any clue why it doesnt work?
You can try this ^\d+$. \d matches digits. The one you wrote does not work because you are using /d instead of \d.
Since you want to disallow whitespace and other characters, why don't you try ^\d+$ and inverse the way of evaluation in your code?
Your regex currently means "anything but whitespace, followed by slashes and d-letters, followed by one more of anything but whitespace". A simple ^\d+$ is sufficient.

# 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 \-.

Resources