Perfect solution to validate a password [duplicate] - javascript

This question already has answers here:
regular expression for password with few rules
(3 answers)
Closed 8 years ago.
I took an online JavaScript test where the 3rd problem was as follows:
Complete the checkPassword function, which should check if the password parameter adheres to the following rules:
Must be longer than 6 characters.
Allowed characters are lower or uppercase Latin alphabetic characters (a-z), numbers (0-9), and special characters +, $, #, \, / only.
Must not have 3 or more consecutive numbers (e.g. "pass12p" is fine,
but "pass125p" is not, because it contains "125")
>
My solution was as follows:
function checkPassword(password) {
return /^[a-zA-Z0-9\+\$\\\/#]{6,}$/.test(password) && !/[0-9]{3,}/.test(password);
}
This solution gave me the correct outputs for the given inputs.
But the ultimate test result told that the solution is just 75% correct sadly.
I think the perfect answer is expected to be a solution just with a single regular expression. Is there anyone who can give me a single regular expression that gives the same result? I am not so good at regular expression so please advise.
I appreciate your help in advance.

Just use a negative lookahead at the start.
^(?!.*?\d{3})[a-zA-Z0-9\+\$\\\/#]{6,}$
(?!.*?\d{3}) at the start asserts that the match won't contain atleast three consecutive digits. Then the regex engine tries to match the pattern [a-zA-Z0-9\+\$\\\/#] against the input string 6 or more times only if this condition is satisfied.
DEMO

^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z\d]{6,}$
For consecutive number check
public boolean isValid(final String userName,final String password) {
for(int i=0;(i+2)<userName.length();i++) if(password.indexof(userName.substring(i,i+2))!=-1) return false; return true;
}

Related

JS: regex, $ end of input not able to use [duplicate]

This question already has answers here:
Validate phone number with JavaScript
(30 answers)
Closed 5 years ago.
console.log(/\d+?\d+?\d+?-\d+?\d+?\d+?-\d+?\d+?\d+?\d+?$/.test("555-555-55539"));
Answer --> true
I was looking for false, i am validating phone numbers. e.g. 555-555-5555 is a correct response([0-9])
I am a newbie to regex, can anyone explain what i am doing wrong here?
How about this.
console.log(/\d{3}-\d{3}-\d{4}$/.test("555-555-55539"));
You used wrong quantifiers in your regex. You made them lazy (+?), but it will still match all characters until the next character from regex is found. In case of your last quantifier (just before $) it will match all digits until the end of string is found. Hence it matches not only one digit but all of them. Same thing happens before each hyphen (555555555-5555-555555555 is valid for your regex).

What does this regular expression mean /^[\w0-9.-]{1,}#[A-z0-9]{1,}.[A-z]{3}$/ [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 7 years ago.
Can someone elaborate the following regular expression:
/^[\w0-9.-]{1,}#[A-z0-9]{1,}.[A-z]{3}$/
and also give some sample strings that satisfy this regular expression?
Thanks
Looks like a crude regex to check for an email address. Not the proper complete one, mind you (it's a lot longer).
^ beginning of string
[\w0-9.-] - word character, a digit, a dot or a dash. Doesn't make that much sense as word characters include digits too, so it can be simplified to [\w.-]
{1,} - one or more of those. There is an equivalent +, it's better to use that instead
# - at sign
[A-z0-9] - a terrible idea to mix capital and lower case letters. As it is right now, this means all ascii characters from A to z plus digits
. - any character. I'm guessing it should have been a literal dot - \.
[A-z]{3} - three characters, again as above
$ - end of line
So my guess is that this was a poor's man attempt at email validation. Here is the simplified version with the [A-z] shenanigan fixed:
/^[\w.-]+#[A-Za-z0-9]+\.[A-Za-z]{3}$/
See it in action
As for something which satisfies the original regex - .#A.AAA
You should checkout http://regexper.com which illustrates regular expressions (Note, I fixed the escaping of the period for you):
From the illustration you can see it is checking for:
The start of the string
One or more characters of: a word character, period or dash
Followed by a single "#" symbol
Followed by one or more characters within the ranges of A-z or 0-9
Followed by a period
Followed by three characters within the range of A-z
The end of the string
as #Kayaman mentions, it's a crude regular expression for an email address, though is an encompassing expression to find any valid email.

Regular expression seems to treat one string as multiple substrings [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 7 years ago.
Why the following code returns "ZZZCamelCase"?? Doesn't such regex examine if the string starts and ends with small case a-z? As what I understand, the str variable should match such condition, so the console output should be "ZZZZZZZZZZZZ", but obviously it somehow breaks the str, and examine the substring against the regex. Why? and how can I tell the program to treat "testCamelCase" as one string?
var str = "testCamelCase";
console.log(str.replace(/^[a-z]+/, 'Z')); // ZZZCamelCase
Here you are matching one or more lower case letters. That's going to be 'test' in your string, because after that comes an uppercase 'C'. So only 'test' gets rpelaced by 'ZZZ'
console.log(str.replace(/^[a-z]+/, 'ZZZ')); // ZZZCamelCase
Use
str.replace(/[a-z]/ig, 'Z')
to get 'ZZZZZZZZZZZZ'
You are forgetting, that regex is case sensitive. This means, that [a-z] doesn't capture the whole string. [a-zA-Z] does. So does [\w] including digits from 0-9.

alphanumeric javascript regex failing [duplicate]

This question already has answers here:
RegEx for Javascript to allow only alphanumeric
(22 answers)
Closed 8 years ago.
I am trying to interrupt a form submission to test field for alphanumeric characters. After googling i found a lot of this implementation for regex that everyone claims works great...
if( !jQuery('input[name=myUsername]').val().test(/^[a-zA-Z0-9]+/) ) {
alert('ERROR: Your username contains invalid characters. Please use numbers and letters only. No spaces, no punctuation.');
jQuery('input[name=myUsername]').focus();
return false;
}
However, this ONLY returns false and creates an alert if the value STARTS with a non-alphanumeric character. If i enter "bo$$" it allows it as alphanumeric even tho $ is not an alphanumeric character... If i enter "$$ob" it fails and alerts.
How do i fix this so that it will fail for ANY invalid character in the entire value? I've tried .match() instead of .test() but same issue im assuming it's something in the regex that is wrong
^[a-zA-Z0-9]+$
put $ the end anchor to limit that.Without $ your regex will make a partial match.
$ assert position at end of a line
bo$$ will make a partial match upto bo as $ is not there.
Use anchor $ to avoid matching unwanted character at the end:
/^[a-zA-Z0-9]+$/

Regex for Password Strength [duplicate]

This question already has answers here:
Regex to validate password strength
(11 answers)
Closed 8 years ago.
could someone pleas help me ,
I need an expression for a password for fulfill the following criteria:
at least 8 characters length to a maximum of 15 characters.
at least one letter in upper Case.
at least one letter in lower Case.
at least 1 special character.
at least 1 numeral.
These must be acceptable in any order if possible.
This is an attempt i found but doesn't fulfill the criteria above,i have tried modification but my problem rests in having these in any order and at least one of any of the characters specified ,i have tried reducing each expression below to suit also :
^(?=.*[A-Z].*[A-Z])(?=.*[!##$&*])(?=.*[0-9].*[0-9])(?=.*[a-z].*[a-z].*[a-z]).{8}$
^ Start anchor
(?=.*[A-Z].*[A-Z]) Ensure string has two uppercase letters.
(?=.*[!##$&*]) Ensure string has one special case letter.
(?=.*[0-9].*[0-9]) Ensure string has two digits.
(?=.*[a-z].*[a-z].*[a-z]) Ensure string has three lowercase letters.
.{8} Ensure string is of length 8.
$ End anchor.
There is no duplicate ,please review the marking unless your sure it is a duplicate
try this regex will helps you
/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W\_])[a-zA-Z0-9\W\_]{8,15}$/

Categories