# in Regular Expression - asp.net

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

Related

Allow Hyphens and apostraphes but no other special characters

I have the following validation expresion on an asp.net web form that allows alphanumeric characters, spaces,at least one alpha character, and a minimum of 3 characters and a maximum of 20:
ValidationExpression="(?!^[0-9]$)(?!^[a-zA-Z]$)^([a-zA-Z0-9 _]{3,20})$"
Now I have been asked to allow hyphens and apostraphes but no other special characters.
How can I implement this in my current validation?
This (?!^[0-9'-]$)(?!^[a-zA-Z'-]$)^([a-zA-Z0-9 _'-]{3,20})$?
Well, the main trick here is that - sign should be placed at the end of the character group for it to be parsed as a literal hyphen.
Try this:
(?=.*?[A-Za-z]+)^[a-zA-Z0-9_\-' ]{3,20}$

Regular Expression Validator for Letters and Numbers only

What is the Regular Expression Validator for only Letters and Numbers in asp.net?
I need to enter only 0-9,a-z and A-Z. I don't want to allow any special characters single or double quotes etc. I am using asp.net 3.5 framework.
I tried ^[a-zA-Z0-9]+$ and ^[a-zA-Z0-9]*$. They are not working.
Any help will be appreciated.
Try the following.
^[a-zA-Z0-9]+$
go to this example and also alphanumerics for more
then try this
^[a-zA-Z0-9]*$
If length restriction is necessary use
^[a-zA-Z0-9]{0,50}$
This will match alphanumeric strings of 0 to 50 chars.
you can try this....
^[a-zA-Z0-9]+$
see more info at here
You can define a regular expression as follows,
Regex myRegularExpression = new Regex(" \b^[a-zA-Z0-9]+$\b");
be sure to include System.Text.RegularExpression
and then use the Regex to match it with your user-control as follows,
eg : if your user-control is a textbox
myRegularExpression.isMatch(myTextBox.Text);
Dear English speaking people. With all due respect. A-Z are not the only letters in the world. Please use \w instead of [A-Za-z0-9] if you support other languages in your apps

regular validation expression in asp.net

i have got a code to edit some function. There is a text box in that web application. It using a regular expression validator control to validate the text box. the validation expression is
ValidationExpression="[\w]{3,15}"
it accept all letters,numbers and underscores. but it do not accept special characters like \,/ * . i want to change the above regular expression to accept / .
i hope someone can explain what the above regular expression means and how to change that expression to accept / without affecting current regular expression
i am using asp.net and c#
string ValidationExpression= "[\w/]{3,15}"
[...] match a single character presents in the list between brackets
[...]{3,15} match between 3 and 15 characters presents between brackets
\w match a word character (letter, digit, underscore...)
/ match the character /
So [\w/]{3,15} match a word character or '/' between 3 and 15 times.
You current regular expression can be deconstructed as follows :
[] brackets represents regular expression group. Regex engine will try to match all the characters or group of characters given inside [] with the input string.
\w - Allow all the alpha numberic characters which includes upper case and lower case alphabets and 0 to 9 numbers and and underscore (This does not include other special characters like / or # ,etc ).
{3,15} means minimum 3 and maximum 15 alphanumeric characters must be provided in order to successfully match the string.
To add other charters, you need to add them explicitly. If you want to add / your regex should be like [\w/]{3,15}.
You can learn everything about regex here.

Regex to test if an input string contains a certain number of characters

So, I basically would like to test to see if a string contains a range of alphanumeric characters. It's to be used as a client-side validation and I don't want to prevent users from entering whatever they want. Best to give examples of what should/should not pass validation:
So to be specific, the expression I'm looking for is to test to make sure string contains anywhere from 3 to 10 alphanumeric characters. I'd like to plug into an ASP.NET client side validator.
NOTE: quotes not part of input (but could be!)
" f o o " should pass since there are 3 chars
"f_0_0" should pass
" fo " should not
"F......o......o......b.....a......r" should pass
thx
^([^a-zA-Z0-9]*[a-zA-Z0-9][^a-zA-Z0-9]*){3,10}$
Allows for exactly 3-10 alphanumeric characters, each surrounded by an arbitrary number of non-alphanumeric characters.
(Untested, but it should conform to the JScript subset of the standard .net Regex syntax, as required by the RegularExpressionValidator. Unfortunately, the shorthands \w and \W cannot be used since they include the underscore as an alphanumeric character.)
I'm not familiar with ASP.NET client-side validators, so I'm not sure if you need to do this in a regex, but potentially an easy solution is as follows:
Remove all non-alphanumeric characters (regex replace [^0-9A-Za-z] with nothing).
Check if string length is 3 or greater.

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