C# Alphanumeric Password validation? - c#

Can any one provide me regx validator which validate the following;
Password must be an alphanumeric password i.e. at least 1 number and at least 1 alphabet.
I tried the following, but it did not work.
(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9])$

You are almost there, you just need to add a quantifier for your last expression and it will work fine. So, it should be something like this
(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{2,})$.
Code will look like this
Regex regexObj = new Regex(#"(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{2,})$");
boolean foundMatch = regexObj.IsMatch(passwordString);
You are currently matching a single digit or a letter as [a-zA-Z0-9] matches a single alphanum character. Read about character classes([]) here http://www.regular-expressions.info/charclass.html

Related

Regex pattern is not correct

I have the following regex to match this:
U$MichaelU$P#$asdqwe123P#$ - this is correct; the other two are not
U$NameU$P#$PasswordP#$
U$UserU$P#$ad2P#$
A registration is valid when:
The username is surrounded by "U$"
The username needs to be minimum 3 characters long, start with an uppercase letter, followed only by lowercase letters
The password is surrounded by "P#$"
The password needs to start with minimum 5 alphabetical letters (not including digits) and must end with a digit
My regex is
#"^(U\S)([A-z][a-z]{3,})\1(P#\S)([a-z]{5,}[^\d])([\d]+)\3$"
The problem is that it matches the first one but when I submit to the judge it passes first 2 test but the rest it breaks, could you please tell me where is my mistake.
Hello your regex must be
#"^(U\S)([A-Za-z]{3,})\1(P#\S)([A-Za-z0-9]{5,})\3$"
it works for you

Regular expression thingy

I would like to create a custom regular expression. Which shall track that the first character of a username should be an alphabet.
Followed by alphanumeric or can have maximum one occurrence of a special character (- or _). I can check for username starts with the alphabet with this ^[a-zA-Z]+$ but not sure what to do to check at most one occurrence of a special character. Any ideas are welcome.
Thanks
From what I understood of your post, you want the following to match.
a-afdsafd
aafdsafd
aafdsa_fd
aafdsa-fd
aAfdsa-FD
And the following to not match:
aa-dsa-fd
aa-dsa_fd
-afdsafd
_afdsafd
Try /^[a-z](?:(?![a-z]+[\-_])[\-_])?[a-z]+(?:(?<![a-z]+[\-_])[\-_]?)[a-z]+?$/i
The i modifier enables case-insensitive matching.
The ^ and $ anchors ensure that the entire string matches our regex.
[a-z] checks that the first character is an alphabet.
(?:(?![a-z]+[\-_])[\-_])?) looks ahead to check that there is no "special character" used later and if there is none, we optionally match one special character.
[a-z]+ Match one or more alphabets.
(?:(?<![a-z]+[\-_])[\-_]?) does the same thing as 4 except it looks behind.
[a-z]+? Optionally match one or more alphabets.
https://regexr.com/3t86l
Edit: I noticed that aAfdsaFd_ should also match. The above does not match this. Slightly modifying #Wiktor Stribiżew's comment, ^[a-zA-Z][a-zA-Z0-9]*(?:[-_][a-zA-Z0-9]*)?$ seems to work fine with all cases. That's cleaner and more efficient. All credit to #Wiktor Stribiżew.
You could match an upper or lowercase character from the start of the string ^[a-zA-Z], match zero or more times alphanumeric [a-zA-Z0-9]* followed by an optional hyphen or underscore [-_]?.
At the end match zero or more times alphanumeric [a-zA-Z0-9]*$ until the end of the string.
^[a-zA-Z][a-zA-Z0-9]*[-_]?[a-zA-Z0-9]*$

Writing a proper regex to allow number and only combinations of letters and numbers mixed up

I have a string example which looks like this:
51925120851209567
The length of the string and numbers may vary, however I want to only enable the string to contain just either numbers, or for it to be a combination of letters and numbers. For example a valid one would be something like this:
B0031Y4M8S // contains combination of letters and numbers without white space
Invalid regex would be:
Does not apply // this one contains white spaces and has only letters
To summarize things up, the regex should allow only these combinations:
51925120851209567 // contains only numbers and is valid
B0031Y4M8S // contains combination of numbers and letters and is valid as well
Everything else is invalid...
The current solution that I have covers only for the string to be a set of integers and nothing else... However I'm not really sure how to filter out combination of numbers and letters without white spaces and special charachters to be valid as well for the regex?
Regex regex = new Regex("^[0-9]+$");
if (regex.IsMatch(parameter))
{
// allow if statement to pass if the regex matches
}
Can someone help me out ?
You may use
^(?![A-Za-z]+$)[0-9A-Za-z]+$
It matches 1+ alphanumeric chars but will fail a match if all string consists of just letters.
Details
^ - start of a string
(?![A-Za-z]+$) - a negative lookahead that fails the match if there are 1+ ASCII letters followed with the end of string immediately to the right of the current location
[0-9A-Za-z]+ - 1+ ASCII letters
$ - end of string.
See the regex demo.
#The fourth bird's answer will almost get you there. I'm no regex expert, but an easy way to get you what you want would be to use:
Regex regex = new Regex("^[a-zA-Z0-9]+$");
This will get you the first level of exclusion. If it passes that, then check with:
Regex regex = new Regex("^[a-zA-Z]+$");
If it matches that, then you know it's only alphabetical characters and you can skip it. I'm sure there's a better way to code golf this one out, but this should work for now if you're in a crunch.

Regular Expression to not allow 3 consecutive characters

I have the following regex:
Regex pattern = new Regex(#"^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,20}/(.)$");
(?=.*\d) //should contain at least one digit
(?=.*[a-z]) //should contain at least one lower case
(?=.*[A-Z]) //should contain at least one upper case
[a-zA-Z0-9]{8,20} //should contain at least 8 characters and maximum of 20
My problem is I also need to check if 3 consecutive characters are identical. Upon searching, I saw this solution:
/(.)\1\1/
However, I can't make it to work if I combined it to my existing regex, still no luck:
Regex(#"^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,20}$/(.)\1\1/");
What did I missed here? Thanks!
The problem is that /(.)\1\1/ includes the surrounding / characters which are used to quote literal regular expressions in some languages (like Perl). But even if you don't use the quoting characters, you can't just add it to a regular expression.
At the beginning of your regex, you have to say "What follows cannot contain a character followed by itself and then itself again", like this: (?!.*(.)\1\1). The (?! starts a zero-width negative lookahead assertion. The "zero-width" part means that it does not consume any characters in the input string, and the "negative lookahead assertions" means that it looks forward in the input string to make sure that the given pattern does not appear anywhere.
All told, you want a regex like this:
new Regex(#"^(?!.*(.)\1\1)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,20}$")
I solved by using trial and error:
Regex pattern = new Regex(#"^(?!.*(.)\1\1)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,20}$");

Regular expression for DataAnnotation for Password

We have a requirement to make sure password conforms to the specific strength format (this is configured in web.config). Requirements are that password must contain certain number of capital characters and certain number of non-alphanumeric characters. I want to annotate my Password property with regular expression that validates password to make sure password contains x number of CAPS and y number of non-alpha chars. Please help with regular expression.
Checking multiple conditions like this in a single regex is best accomplished using lookaheads, for example say you want 3 capital characters and 4 non-alpha characters, you could use the following regex:
^(?=(?:.*[A-Z]){3})(?=(?:.*[^a-zA-Z]){4})
Explanation: first, lets think about what a regex would look like that only checks the first condition. To match 3 uppercase characters we can use the following:
(?:.*[A-Z]){3}
We can still check this condition by dropping it inside of a lookahead, which is what the (?=...) does, so now (?=(?:.*[A-Z]){3}) checks this condition without consuming any characters. At this point we can check the second condition using (?:.*[^a-zA-Z]){4}. I put this second condition inside of a lookahead as well so that adding more checks is straightforward.
Note that the current regex won't actually match any characters, it will match the beginning of the string (zero characters) if all conditions match, otherwise the match will fail. If you want it to actually consume characters as well, just add .* to the end.
I found a good article that solves it for me
http://www.zorched.net/2009/05/08/password-strength-validation-with-regular-expressions/

Categories

Resources