Regular Expression Validator for Letters and Numbers only - asp.net

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

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}$

Regex in asp.net for allowing only characters with single space?

I want to allow a user to enter only characters and with single space in a textbox. What i have tried is :
^[\w\.:\(\)\[\]{}\-_](?: ?[\w\.:\(\)\[\]{}\-_])*$
it is blocking all but allowing digits also . How to make it correct such that it only allow characters and no digits but a single space between words? Thank you.
I'd use:
^[\p{L}.:()[\]{}_-]+(?: [\p{L}.:()[\]{}_-]+)*$
Where \p{L} stands for any letter.
Edit:
I've changed \pL to \p{L} because it's not supported by .NET.
Thanks to Alan Moore.
Is ([A-Za-z])+( [A-Za-z]+) what you're after? Or do you want the regex to only match once?
Assuming there must be either oneor two 'words' (i.e. sequences of non-space characters)
"\s*[A-Za-z] +(\s[A-Za-z] +)?\s*"

Issue with regex in ASP.net for german, french & spanish languages

I want to support German, French & Spanish characters on a particular field of my website. I need a regex for this. Presently I am using -
^[\w\s-\+\$\*\.\?\:\;\!\,"'\%\&\/\(\)\#\#«»£°¿¡_ÀÂÆÇÈÉÊËÎÏÔŒÙÛÜàâæçèéêëîïôœùûüÄÖäößÁÍÑÓÚáíñóú\u201E\u201C\u201D\u20AC]{1,255}$
This regex basically uses all the char set from the 3 languages I mentioned.
Is there a neat way to avoid this lengthy regex? I tried /p{L}/p{Z} regex. However this didnt work.
My website is in ASP.net
/p{L}/p{Z} is wrong, should be \p{L}\{Z}.
all the letters, like "ÀÂÆÇÈ" shouldn't be needed, they are all included in \w in .net!
You don't need most of the escaping in a character class
You can't write something like " in a character class, only thing what happens is that every single character is added to the class.
This should be quite similar to what you used:
^[-\p{L}\p{N}\p{P}\p{Z}_+$*%&/##«»£°\u201E\u201C\u201D\u20AC]{1,255}$
I haven't checked those Unicode codepoints at the end of the class, I don't now if they are needed or not.
For an explanation of all the \p{...} items see Unicode Regular Expressions on regular-expressions.info

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.

Regular Expression for username and password?

I am trying to use a regular expression for name field in the asp.net application.
Conditions:name should be minimum 6 characters ?
I tried the following
"^(?=.*\d).{6}$"
I m completely new to the regex.Can any one suggest me what must be the regex for such condition ?
You could use this to match any alphanumeric character in length of 6 or more: ^[a-zA-Z0-9]{6,}$. You can tweak it to allow other characters or go the other route and just put in exclusions. The Regex Coach is a great environment for testing/playing with regular expressions (I wrote a blog post with some links to other tools too).
Look at Expression library and choose user name and/or password regex for you. You can also test your regex in online regex testers like RegexPlanet.
My regex suggestions are:
^[a-zA-Z][a-zA-Z0-9._\-]{5,}$
This regex accepts user names with minimum 6 characters, starting with a letter and containing only letters, numbers and ".","-","_" characters.
Next one:
^[a-zA-Z0-9._\\-]{6,}$
Similar to above, but accepts ".", "-", "_" and 0-9 to be first characters too.
If you want to validate only string length (minimum 6 characters), this simple regex below will be enough:
^.{6,}$
What about
^.{6,}$
What's all the stuff at the start of yours, and did you want to limit yourself to digits?
NRegex is a nice site for testing out regexes.
To just match 6 characters, ".{6}" is enough
In its simplest form, you can use the following:
.{6,}
This will match on 6 or more characters and fail on anything less. This will accept ANY character - unicode, ascii, whatever you are running through. If you have more requirements (i.e. only the latin alphabet, must contain a number, etc), the regex would obviously have to change.

Resources