javascript Regex to have atmost 5 characters in password - javascript

I am trying to implement maximum case in JavaScript for password validation.
I have a scenario where in password user should enter maximum 5 characters (a-z,A-Z) and password length having no restriction
Passsword length no limit
except (a-z,A-Z) ,there is no limtation
charcter(a-z,A-Z) will have atmost 5 in password .Not more than that.Sequence doesnot matter.
I tried /[^A-Z,a-z]*(?:[A-Z,a-z][^A-Z,a-z]*){0,5}/
But it is not working.
Kindly help

Did you even search? It is obviously, that this is a duplicate.
See
Regex javascript for Minimum 8 characters,at least one number and one special character, maximum 32 characters
Password REGEX with min 6 chars, at least one letter and one number and may contain special characters
JavaScript regex for alphanumeric string with length of 3-5 chars

You can just count how many characters you have in the password like so:
if(password.match(/[a-zA-Z]/g).length > 5){ /* reject */ }

You can use the below regex for password validation.
((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[##$%]).{5,20})
It validates :
Must contain ! digit [0-9]
One lowercase character [a-z]
One uppercase character [A-Z]
One symbol [##$%]
Length of password must be between [5-20].

Break it down and solve it step by step
Alphabet only - define your character set
/[A-Za-z]/
Maximum length of 5 - use a quantifier
/[A-Za-z]{0,5}/
Nothing else is allowed - wrap it with ^ and $
/^[A-Za-z]{0,5}$/

Related

Regex for Password Validation Field

I'm trying to validate a password in my application by using Regex. I don't have much idea of Regex, This is what i got.
"^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!#$%^&*-]).{8,}$"
This expression is also checking Special Character, and 1 upper case character in password.
I need a Regex that full fills following criteria.
Password Must be at least 8 characters Long.
Password must contain 1 Letter
Password must contain 1 Number
Password should not contain any special Character.
I won't claim to be a regex master, but for the password criteria you describe, you could use:
/^(?=.*?[a-zA-Z])(?=.*\d)([a-zA-Z0-9])+$/ to test for conditions 2, 3, and 4.
For condition 1, I would just check the string length property to make sure it's at least 8 characters.
Note that in your current regex, you are requiring at least one uppercase letter, at least one lowercase letter, at least one digit, and at least one of the specified special characters (#?!#$%^&*-). Hope this helps.

Check password valid using regex in javascript

The password must be eight characters or longer
The password must contain at least 2 lowercase alphabetical character.
The password must contain at least 2 uppercase alphabetical character.
The password must contain at least 2 numeric character.
The password must contain at least 2 special character.
My code
function checkPass(pw) {
var regx = new RegExp("^(?=.*[a-z]{2})(?=.*[A-Z]{2})(?=.*[0-9]{2})(?=.*[!##\$%\^&\*\)\(]{2})(?=.{8,})");
return regx.test(pw);
}
checkPass('PAssword12#$') => true
checkPass('PaSsword12#$') => false
I want to funtion return true when 2 uppercase character is not sequential.
Thanks!
You need to use the $ anchor to check for length (and best is to move that check out from lookaheads) and to allow some non-uppercase letters in between like this:
function checkPass(pw) {
var regx = /^(?=(?:[^a-z]*[a-z]){2})(?=(?:[^A-Z]*[A-Z]){2})(?=(?:\D*\d){2})(?=(?:[^!##$%^&*)(]*[!##$%^&*)(]){2}).{8,}$/;
return regx.test(pw);
}
document.write(checkPass('PAssword12#$') + "<br>");
document.write(checkPass('PaSsword12#$'));
Note that I used the principle of contrast: (?:[^a-z]*[a-z]){2} matches 2 sequences of symbols other than a-z zero or more times followed by 1 lowercase letter. I modified all the lookaheads the same way.
Rather than having [A-Z]{2} which will only match two uppercase characters together, you'll have to put in an optional match for any other characters in between two separate ranges (you'll have to do this for the lowercase, numbers and symbols as well). So you would instead put
[A-Z].*?[A-Z]
You also don't actually need to check whether it's at least 8 characters, because any password meeting the criteria for lowercase/uppercase letters, numbers and symbols has to be 8 characters at minimum anyway.

JS - Regex for name

The username should contain only alphabatic chars (a-z) up to 25 chars.
The first and the last space should be removed.
The username can be more then 1 word, not a limit for how many words the name counts
So the regex I am using now is almost valid, except that a username with 3 words or more isn't valid. And this should be valid.
Valid names can be:
Dennis
Dennis is
Dennis is cool
Dennis is the coolest
etc.. up to 25 chars because this is the max length.
This is the current regex I am using:
var pattern = /^[a-z\u00C0-\u01FF]+([\s\.\-\']?[a-z\u00C0-\u01FF]+)? $/i;
if (pattern.exec(uname) == null)
{
alert('Invalid name');
return false;
}
So how to adjust the regex to make it work for more words then 2 with a maximum length of 25 chars?
Your regex specifies it requires one set of characters, then optionally, a space and another set of characters. Hence your two word limit.
If you changed ([\s\.\-\']?[a-z\u00C0-\u01FF]+)? to ([\s\.\-\']?[a-z\u00C0-\u01FF]+)*, it would allow zero or more additional words, rather than zero or one.

Password REGEX with min 6 chars, at least one letter and one number and may contain special characters

I need a regular expression with condition:
min 6 characters, max 50 characters
must contain 1 letter
must contain 1 number
may contain special characters like !##$%^&*()_+
Currently I have pattern: (?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{6,50})$
However it doesn't allow special characters, does anybody have a good regex for that?
Thanks
Perhaps a single regex could be used, but that makes it hard to give the user feedback for which rule they aren't following. A more traditional approach like this gives you feedback that you can use in the UI to tell the user what pwd rule is not being met:
function checkPwd(str) {
if (str.length < 6) {
return("too_short");
} else if (str.length > 50) {
return("too_long");
} else if (str.search(/\d/) == -1) {
return("no_num");
} else if (str.search(/[a-zA-Z]/) == -1) {
return("no_letter");
} else if (str.search(/[^a-zA-Z0-9\!\#\#\$\%\^\&\*\(\)\_\+]/) != -1) {
return("bad_char");
}
return("ok");
}
following jfriend00 answer i wrote this fiddle to test his solution with some little changes to make it more visual:
http://jsfiddle.net/9RB49/1/
and this is the code:
checkPwd = function() {
var str = document.getElementById('pass').value;
if (str.length < 6) {
alert("too_short");
return("too_short");
} else if (str.length > 50) {
alert("too_long");
return("too_long");
} else if (str.search(/\d/) == -1) {
alert("no_num");
return("no_num");
} else if (str.search(/[a-zA-Z]/) == -1) {
alert("no_letter");
return("no_letter");
} else if (str.search(/[^a-zA-Z0-9\!\#\#\$\%\^\&\*\(\)\_\+\.\,\;\:]/) != -1) {
alert("bad_char");
return("bad_char");
}
alert("oukey!!");
return("ok");
}
btw, its working like a charm! ;)
best regards and thanks to jfriend00 of course!
Check a password between 7 to 16 characters which contain only characters, numeric digits, underscore and first character must be a letter-
/^[A-Za-z]\w{7,14}$/
Check a password between 6 to 20 characters which contain at least one numeric digit, one uppercase, and one lowercase letter
/^(?=.\d)(?=.[a-z])(?=.*[A-Z]).{6,20}$/
Check a password between 7 to 15 characters which contain at least one numeric digit and a special character
/^(?=.[0-9])(?=.[!##$%^&])[a-zA-Z0-9!##$%^&]{7,15}$/
Check a password between 8 to 15 characters which contain at least one lowercase letter, one uppercase letter, one numeric digit, and one special character
/^(?=.\d)(?=.[a-z])(?=.[A-Z])(?=.[^a-zA-Z0-9])(?!.*\s).{8,15}$/
I hope this will help someone. For more please check this article and this site regexr.com
A more elegant and self-contained regex to match these (common) password requirements is:
^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d^a-zA-Z0-9].{5,50}$
The elegant touch here is that you don't have to hard-code symbols such as $ # # etc.
To accept all the symbols, you are simply saying: "accept also all the not alphanumeric characters and not numbers".
Min and Max number of characters requirement
The final part of the regex {5,50} is the min and max number of characters, if the password is less than 6 or more than 50 characters entered the regex returns a non match.
I have a regex, but it's a bit tricky.
^(?:(?<Numbers>[0-9]{1})|(?<Alpha>[a-zA-Z]{1})|(?<Special>[^a-zA-Z0-9]{1})){6,50}$
Let me explain it and how to check if the tested password is correct:
There are three named groups in the regex.
1) "Numbers": will match a single number in the string.
2) "Alpha": will match a single character from "a" to "z" or "A" to "Z"
3) "Special": will match a single character not being "Alpha" or "Numbers"
Those three named groups are grouped in an alternative group, and {6,50} advises regex machine to capture at least 6 of those groups mentiond above, but not more than 50.
To ensure a correct password is entered you have to check if there is a match, and after that, if the matched groups are capture as much as you desired. I'm a C# developer and don't know, how it works in javascript, but in C# you would have to check:
match.Groups["Numbers"].Captures.Count > 1
Hopefully it works the same in javascript! Good luck!
I use this
export const validatePassword = password => {
const re = /^(?=.*[A-Za-z])(?=.*\d)[a-zA-Z0-9!##$%^&*()~¥=_+}{":;'?/>.<,`\-\|\[\]]{6,50}$/
return re.test(password)
}
DEMO https://jsfiddle.net/ssuryar/bjuhkt09/
Onkeypress the function triggerred.
HTML
<form>
<input type="text" name="testpwd" id="testpwd" class="form=control" onkeyup="checksPassword(this.value)"/>
<input type="submit" value="Submit" /><br />
<span class="error_message spassword_error" style="display: none;">Enter minimum 8 chars with atleast 1 number, lower, upper & special(##$%&!-_&) char.</span>
</form>
Script
function checksPassword(password){
var pattern = /^.*(?=.{8,20})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[##$%&!-_]).*$/;
if(!pattern.test(password)) {
$(".spassword_error").show();
}else
{
$(".spassword_error").hide();
}
}
International UTF-8
None of the solutions here allow international letters, i.e. éÉöÖæÆóÓúÚáÁ, but are mainly focused on the english alphabet.
The following regEx uses unicode, UTF-8, to recognise upper and lower case and thus, allow international characters:
// Match uppercase, lowercase, digit or #$!%*?& and make sure the length is 6 to 50 in length
const pwdFilter = /^(?=.*\p{Ll})(?=.*\p{Lu})(?=.*[\d|##$!%*?&])[\p{L}\d##$!%*?&]{6,50}$/gmu
if (!pwdFilter.test(pwd)) {
// Show error that password has to be adjusted to match criteria
}
This regEx
/^(?=.*\p{Ll})(?=.*\p{Lu})(?=.*[\d|##$!%*?&])[\p{L}\d##$!%*?&]{6,50}$/gmu
checks if an uppercase, lowercase, digit or #$!%*?& are used in the password. It also limits the length to be 6 minimum and maximum 50 (note that the length of 😀🇺🇸🇪🇸🧑‍💻 emojis counts as more than one character in the length).
The u in the end, tells it to use UTF-8.
First, we should make the assumption that passwords are always hashed (right? always hashed, right?). That means we should not specify the exact characters allowed (as per the 4th bullet). Rather, any characters should be accepted, and then validate on minimum length and complexity (must contain a letter and a number, for example). And since it will definitely be hashed, we have no concerns over a max length, and should be able to eliminate that as a requirement.
I agree that often this won't be done as a single regex but rather a series of small regex to validate against because we may want to indicate to the user what they need to update, rather than just rejecting outright as an invalid password. Here's some options:
As discussed above - 1 number, 1 letter (upper or lower case) and min 8 char. Added a second option that disallows leading/trailing spaces (avoid potential issues with pasting with extra white space, for example).
^(?=.*\d)(?=.*[a-zA-Z]).{8,}$
^(?=.*\d)(?=.*[a-zA-Z])\S.{6,}\S$
Lastly, if you want to require 1 number and both 1 uppercase and 1 lowercase letter, something like this would work (with or without allowing leading/trailing spaces)
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}$
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])\S.{6,}\S$
Lastly as requested in the original post (again, don't do this, please try and push back on the requirements!!) - 1 number, 1 letter (upper or lower case), 1 special char (in list) and min 8 char, max 50 char. Both with/without allowing leading/trailing spaces, note the min/max change to account for the 2 non-whitespace characters specified.
^(?=.*\d)(?=.*[a-zA-Z])(?=.*[!##$%^&*()_+]).{8,50}$
^(?=.*\d)(?=.*[a-zA-Z])(?=.*[!##$%^&*()_+])\S.{6,48}\S$
Bonus - separated out is pretty simple, just test against each of the following and show the appropriate error in turn:
/^.{8,}$/ // at least 8 char; ( /^.{8,50}$/ if you must add a max)
/[A-Za-z]/ // one letter
/[A-Z]/ // (optional) - one uppercase letter
/[a-z]/ // (optional) - one lowercase letter
/\d/ // one number
/^\S+.*\S+$/ // (optional) first and last character are non-whitespace)
Note, in these regexes, the char set for a letter is the standard English 26 character alphabet without any accented characters. But my hope is this has enough variations so folks can adapt from here as needed.
// more secure regex password must be :
// more than 8 chars
// at least one number
// at least one special character
const PASSWORD_REGEX_3 = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!##$%^&*]).{8,}$/;

Regular expression to validate textbox length

I want a regular expression to validate an ASP textbox field with the minimum length of 11 characters and in the middle of the string should be a "-" sign. The sample string is: "0000-011111". I want to validate the textbox to make sure user enters a minimum of 10 numbers with "-" sign after 4 digits using regular expressions. Please help me.
Thank you.
Use
\d{4}-\d{6}
\d represents a digit, - is a literal dash and the number in the curly brackets force the preceeding token to be present the given number of times.
^\d{4}-\d{6,}$
You should use also ^ at the beginning and $ at the end to ensure that there is nothing before and after your string that you don't want to have. Also important is the {6,} so it will match at least 6 digits, without , it will match exactly 6 digits. If you want set a maximum of digits you can specify after the ,, e.g. {6,20}.

Categories