Regular Expression to validate Alphanumeric value in JavaScript [duplicate] - javascript

This question already has answers here:
alphanumeric regex javascript
(3 answers)
Closed 9 years ago.
I want to check if a string is STRICTLY ALPHANUMERIC in javascript. A valid string should not contain any special characters and must have atleast one uppercase or lowercase alphabet and a digit.
aaa, 1111, 112##, 12qw# : Invalid
aaa111, 12abc, abc123abc, 12bc12 : valid
Can anyone help me to frame my regular expression?

Using /^(\d+[a-z]|[a-z]+\d)[a-z\d]*$/i:
> /^(\d+[a-z]|[a-z]+\d)[a-z\d]*$/i.test('111aa')
true
> /^(\d+[a-z]|[a-z]+\d)[a-z\d]*$/i.test('aaa222')
true
> /^(\d+[a-z]|[a-z]+\d)[a-z\d]*$/i.test('aaa')
false
> /^(\d+[a-z]|[a-z]+\d)[a-z\d]*$/i.test('222')
false
> /^(\d+[a-z]|[a-z]+\d)[a-z\d]*$/i.test('112aaa#')
false

/^[a-z0-9]*([a-z]\d|\d[a-z])[a-z0-9]*$/i
The import bit is in the middle: it's either a letter followed by a digit, or a digit followed by a letter. If a valid string contains at least one of each, there must be part of the string where this is true. Then we just pad on both sides with zero or more alphanumeric characters.
See http://regex101.com/r/wZ0qL2

Related

regex - minimum of 6 characters including 2 special characters [duplicate]

This question already has answers here:
Regular expression to match at least two special characters in any order
(3 answers)
javascript regex for special characters
(15 answers)
Closed 3 years ago.
I am not very confortable with regex. I would like the password to have a minimum of 6 characters, including 2 special characters
I tried a first regex but it just returns false each time...
console.log(/^[a-zA-Z0-9!$#%]+$/.test(this.form.password));
I can test the number of characters another way but I would like to include the two special characters through the regex.
Thanks a lot in advance for your help
First lookahead for 6 characters, to ensure that enough exist in the input. Then, repeat twice: 0 or more normal characters, followed by a special character, to ensure that there are at least 2 special characters in the input. Then match 0 or more [special or normal] characters.
/^(?=.{6})(?:[a-z\d]*[!$#%]){2}[a-z\d!$#%]*$/i
https://regex101.com/r/ZyFZPm/2
^ - Start of string
(?=.{6}) - At least 6 characters
(?:[a-z\d]*[!$#%]){2} - Repeat twice:
[a-z\d]* - 0+ alphanumeric characters
[!$#%] - 1 special character
[a-z\d!$#%]* - Match both special and alphanumeric characters up to the end of the string
$ - End of string

Can somebody tell me how can this regular expression match anything? [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 6 years ago.
This is the javascript regular expression I'm confused about. I know that (?=) is the positive lookahead, but is there suppose to have a main expression before that?
/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])\w{8,}$/
The answer says it matches a password which is:
at least
one number, one lowercase and one uppercase letter and at least 8 characters that
are letters, numbers or the underscore
But I don't see why. Can somebody explain a little?
Let's break it down:
/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])\w{8,}$/
^ // Match the start of the string
(?=.*\d) // Make sure the string contains at least one digit
(?=.*[a-z]) // Make sure the string contains at least one lowercase letter
(?=.*[A-Z]) // Make sure the string contains at least one uppercase letter
\w{8,} // Match at least eight word characters (alphanumeric or underscore)
$ // Match the end of the string
(?=.*PATTERN) is a common way to ensure that a match string contains PATTERN.
It works because .* matches anything (except newline characters); the lookahead literally means "This regex should only match if you find PATTERN after something."

Javascript regex for username containing letters, numbers and one hyphen [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 7 years ago.
I'm going to check the username via both JavaScript then PHP.
The username can contain English letters, numbers and one hyphen (-).
The username cannot be started with hyphen (-).
The username cannot be finished with hyphen (-).
The username cannot be started with numbers.
The username cannot contain more than one hyphen (-).
The username cannot be shorter than 6 and longer than 20.
abc123 is correct.
abc-123 is correct.
ab12 is wrong: username is shorter than 6 character.
-abc123 is wrong: username is started with hyphen.
abc123- is wrong: username is finished with hyphen.
ab-12-c3 is wrong: username contains more than one hyphen.
123abc is wrong: username is started with numbers.
You can use
^(?!\d)(?!.*-.*-)(?!.*-$)(?!-)[a-zA-Z0-9-]{6,20}$
See demo
Explanation:
^ - Match at the beginning
(?!\d) - Do not match if the string starts with a digit
(?!.*-.*-) - Do not match if the string has 2 hyphens in the string
(?!.*-$) - Do not match if the string ends with a hyphen
(?!-) - Do not match if the string starts with a hyphen
[a-zA-Z0-9-]{6,20} - Match 6 to 20 characters from the range specified
$ - Assert the end of string.
You need to use a lookahead based regular expression:
/^(?=.{6,20}$)[a-z][a-z0-9]+(?:-[a-z0-9]+)?$/i
A logical approach using JavaScript, would be:
function _isvalid(str) {
var re = /^[a-z][a-z0-9]+(?:-[a-z0-9]+)?$/i
return ((str.length > 5) && (str.length < 21) && re.test(str))
}

Regex Password Validation - Codewars [duplicate]

This question already has answers here:
Regex for password must contain at least eight characters, at least one number and both lower and uppercase letters and special characters
(42 answers)
Regular Expression for alphanumeric with first alphabet and at least one alphabet and one number
(2 answers)
Closed 7 years ago.
Disclaimer: This is a Codewars problem.
You need to write regex that will validate a password to make sure it
meets the following criteria:
At least six characters long
contains a lowercase letter
contains an uppercase letter
contains a number
Valid passwords will only be
alphanumeric characters.
So far, this is my attempt:
function validate(password) {
return /^[A-Za-z0-9]{6,}$/.test(password);
}
What this does so far is make sure that each character is alphanumeric and that the password has at least 6 characters. It seems to work properly in those regards.
I am stuck on the part where it requires that a valid password have at least one lowercase letter, one uppercase letter, and one number. How can I express these requirements along with the previous ones using a single regular expression?
I could easily do this in JavaScript, but I wish to do it through a regular expression alone since this is what the problem is testing.
You need to use lookaheads:
function validate(password) {
return /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])[A-Za-z0-9]{6,}$/.test(password);
}
Explanation:
^ # start of input
(?=.*?[A-Z]) # Lookahead to make sure there is at least one upper case letter
(?=.*?[a-z]) # Lookahead to make sure there is at least one lower case letter
(?=.*?[0-9]) # Lookahead to make sure there is at least one number
[A-Za-z0-9]{6,} # Make sure there are at least 6 characters of [A-Za-z0-9]
$ # end of input

Perfect solution to validate a password [duplicate]

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

Categories