Regex to test if an input string contains a certain number of characters - asp.net

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.

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

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

ASP.NET regular expression to restrict consecutive characters

Using ASP.NET syntax for the RegularExpressionValidator control, how do you specify restriction of two consecutive characters, say character 'x'?
You can provide a regex like the following:
(\\w)\\1+
(\\w) will match any word character, and \\1+ will match whatever character was matched with (\\w).
I do not have access to asp.net at the moment, but take this console app as an example:
Console.WriteLine(regex.IsMatch("hello") ? "Not valid" : "Valid"); // Hello contains to consecutive l:s, hence not valid
Console.WriteLine(regex.IsMatch("Bar") ? "Not valid" : "Valid"); // Bar does not contain any consecutive characters, so it's valid
Alexn is right, this is the way you match consecutive characters with a regex, i.e. (a)\1 matches aa.
However, I think this is a case of everything looking like a nail when you're holding a hammer. I would not use regex to validate this input. Rather, I suggest validating this in code (just looping through the string, comparing str[i] and str[i-1], checking for this condition).
This should work:
^((?<char>\w)(?!\k<char>))*$
It matches abc, but not abbc.
The key is to use so called "zero-width negative lookahead assertion" (syntax: (?! subexpression)).
Here we make sure that a group matched with (?<char>\w) is not followed by itself (expressed with (?!\k<char>)).
Note that \w can be replaced with any valid set of characters (\w does not match white-spaces characters).
You can also do it without named group (note that the referenced group has number 2):
^((\w)(?!\2))*$
And its important to start with ^ and end with $ to match the whole text.
If you want to only exclude text with consecutive x characters, you may use this
^((?<char>x)(?!\k<char>)|[^x\W])*$
or without backreferences
^(x(?!x)|[^x\W])*$
All syntax elements for .NET Framework Regular Expressions are explained here.
You can use a regex to validate what's wrong as well as what's right of course. The regex (.)\1 will match any two consecutive characters, so you can just reject any input that gives an IsValid result to that. If this is the only validation you need, I think this way is far easier than trying to come up with a regex to validate correct input instead.

Regular Expression Password Validator

I'm having a hard time trying to create a right regular expression for the RegularExpressionValidator control that allows password to be checked for the following:
- Is greater than seven characters.
- Contains at least one digit.
- Contains at least one special (non-alphanumeric) character.
Cant seem to find any results out there too. Any help would be appreciated! Thanks!
Maybe you will find this article helpful. You may try the following expression
^.*(?=.{8,})(?=.*[\d])(?=.*[\W]).*$
and the breakdown:
(?=.{8,}) - contains at least 8 characters
(?=.*[\d]) - contains at least one digit
(?=.*[\W]) - contains at least one special character
http://msdn.microsoft.com/en-us/library/ms972966.aspx
Search for "Lookaround processing" which is necessary in these examples. You can also test for a range of values by using .{4,8} as in Microsoft's example:
^(?=.*\d).{4,8}$
Try this
((?=.*\d)(?=.*[a-z])(?=.*[\W]).{6,20})
Description of above Regular Expression:
( # Start of group
(?=.*\d) # must contains one digit from 0-9
(?=.*[a-z]) # must contains one lowercase characters
(?=.*[\W]) # must contains at least one special character
. # match anything with previous condition checking
{7,20} # length at least 7 characters and maximum of 20
) # End of group
"/W" will increase the range of characters that can be used for password and pit can be more safe.
Use for Strong password with Uppercase, Lowercase, Numbers, Symbols & At least 8 Characters.
//Code for Validation with regular expression in ASP.Net core.
[RegularExpression(#"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\da-zA-Z]).{8,15}$")]
Regular expression password validation:
#"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\da-zA-Z]).{8,15}$"

Resources