regular expression for password [duplicate] - javascript

This question already has answers here:
Regular Expression for password validation
(6 answers)
Closed 9 years ago.
I need help coming up with a regular expression for javascript which has to verify a password.
The password (8 characters long, at least one non-letter character)
it would also be useful to make sure the user does not have spaces at the beginning or end

This is a pattern that will match what you asked for
pattern = /^(?=.*[^a-z])\S.{6}\S$/i;

Depends how 'complicated' you want to get but I find either if the following useful:
/*
No more than 4 same characters
- One digit
- One uppercase
- One lowercse
- One 'punctuation' mark
- between 8 and 20 characters
*/
re = /^(?!.*(.)\1{4})((?=.*[\d])(?=.*[A-Z])(?=.*[a-z])(?=.*[^\w\d\s])).{8,20}$/;
/*
- One Digit
- One lower case
- One upper case
- Maximum 2 repeating char
- between 6 and 20 characters
*/
re = /^(?!.*(.)\1{2})((?=.*[a-z])(?=.*[A-Z])(?=.*\d)).{6,20}$/;
These are not my regexp's as I found them on the web but can't remember where.

Related

Having 2 possible cases in regex [duplicate]

This question already has answers here:
Regex using javascript to return just numbers
(14 answers)
Closed 2 years ago.
I have 2 cases of outcome of a string, and I want to get the numbers out of it.
The first one is <#&!302050872383242240>
And the second one is <#&302050872383242240>
Is it possible to get only the numbers of this regex or remove <#&!> and <#&> out of this string?
Try:
^<#&\!?(\d+)>$
^ asserts position at start of a line
\!? matches the character ! literally (case sensitive)
? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed
1st Capturing Group (\d+)
\d+ matches a digit (equal to [0-9])
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed
Demo

Why is my regex allowing unspecified characters? JavaScript [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Difference between regex [A-z] and [a-zA-Z]
(6 answers)
Regular expression works on regex101.com, but not on prod
(1 answer)
Closed 2 years ago.
I know this question has been asked a few times and the answers are unique to the specific regexes in question, but aside from that, I've tried to make sure that I'm escaping characters that have special meaning. Whilst this regex plays ball on https://regex101.com (in JavaScript mode), in my app it's having other ideas!
^([A-z0-9][A-z0-9 '\-,\.:\&]{0,245}[A-z0-9]|[A-z0-9][A-z0-9 '\-,\.:\&]{1,244}[A-z0-9])$
This is what I tell the user: 2-247 characters, start and end with A-z 0-9, permitted special characters: ' - , . : &
...but as you see, I'm actually also ensuring that the string starts with:
a) Two non-special characters, or
b) One non-special character followed by a special character, as long as that special character is followed by one or more non-special characters.
This is how I'm implementing the regex:
var nameRegex = new RegExp("^([A-z0-9][A-z0-9 '\-,\.:&]{0,245}[A-z0-9]|[A-z0-9][A-z0-9 '\-,\.:&]{1,244}[A-z0-9])$");
if (!nameRegex.test(formElements[i].value)) {
// validation stuff here
}
Everything the regex intends on doing, it does. I've tested every condition that I'm checking for. But it does more. Regex 101 disallows a string like d*d, but my app? Perfectly fine.
I'll try .match instead of .test, maybe .test isn't the tool I think I need for this job?

Regex query, description [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 5 years ago.
I have a regex query for password verification, the rules are password must be between 8-15 chars, 1number + 1 special characters. It is working perfectly in web form.
I only need to understand it fully. If anyone can help me in describing this regex group by group,, it will be of great help to me. I do understand some part but not all.
^(?=.*[0-9])(?=.*[!##$%^&*])[a-zA-Z0-9!##$%^&*]{7,15}$
Since you updated the regex...
^(?=.*[0-9])(?=.*[!##$%^&*])[a-zA-Z0-9!##$%^&*]{7,15}$
^(?=.*[0-9]) from the start of the string, match any numbers. The lookahead ?= prevents the regex from continuing if nothing matches.
(?=.*[!##$%^&*]) match any special characters in the group.
[a-zA-Z0-9!##$%^&*] capture all letters, numbers, and special characters. At least 7 and up to 15 until the end of the line.

7 or 10 digits input validation using javascript regular expression? [duplicate]

This question already has answers here:
Is there a regex quantifier that says "either x or y repeats"?
(4 answers)
Closed 5 years ago.
I want user to type either 7 digits number or 10 digits number in a textbox.
My code:
var numberfilter = /^([0-9]{7})|([0-9]{10})$/;
var x=document.forms["myForm"]["number_field"].value;
if(numberfilter.test(x)==true)
alert("Valid");
else if(numberfilter.test(x)==false)
alert("Invalid");
The above regular expression is showing "valid" for 7 or more digits also. Please help!
The | operator applies to the whole expressions on the left and right side not just the group in brackets, unless you put it inside a group. So your expression is basically:
^([0-9]{7}) or ([0-9]{10})$
what you need is duplicate the ^$ anchors on both sides of |:
^([0-9]{7})$|(^[0-9]{10})$
or
group the whole thing apart from anchors:
^(([0-9]{7})|([0-9]{10}))$
EDIT: The above explains where you went wrong, but the solution is not the best one. See the reference in comment for the slicker solution:
^(\d{7})(\d{3})?$
try regex
/^([0-9]{7})$|^([0-9]{10})$/ (whole word length is 7 or whole word length is 10)
Your regex actually says "Match if the first 7 characters are numbers OR if the last 10 characters are numbers".
This is because you forgot to place the anchors on both ends of the conditionals. Try something like this.
^([0-9]{7})$|^([0-9]{10})$
You can see it work here.
DEMO
var numberfilter = /^\d{7}(?:\d{3})?$/;
var x='6666666666';
var y='66666666';
var z='6666666';
test(x);
test(y);
test(z);
function test(x){
if(numberfilter.test(x)==true)
console.log(x+" Valid");
else if(numberfilter.test(x)==false)
console.log(x+" Invalid");
}
Here is Regex /^\d{7}(?:\d{3})?$/ checks for first 7 digit with (?: capturing group for next 3 digit

Regex pattern to match this string [duplicate]

This question already has answers here:
regex pattern to match a type of strings
(4 answers)
Closed 8 years ago.
I need to match the below type of strings using a regex pattern in javascript.
E.g. /this/<one or more than one word with hyphen>/<one or more than one word with hyphen>/<one or more than one word with hyphen>/<one or more than one word with hyphen>
So this single pattern should match both these strings:
1. /this/is/single-word
2. /this/is more-than/single/word-patterns/to-be-matched
Only the slash (/)and the 'this' in the beginning are consistent and contains only alphabets.
Try this -
^\/this(?:\/[\w\- ]+)+$
Demo here
There are some inconsistencies in your question, and it's not quite clear exactly what you want to match.
That being said, the following regex will provide a loose starting point for the exact strings that you want.
/this/(?:[\w|-]+/?){1,10}
This assumes the ' ' in your url was not intentional. This example will match a url with '/this/' + 1 to 10 additional '/' chunks.
(?:) -> non-matching group
[\w|-]+ -> one or more word characters or a hyphen
/? -> zero or one slashes
{1,10} -> 1 to 10 of the previous element, the non-matching group

Categories

Resources