Issues in password regular expression - javascript

Hi all I am making a password regular expression in javascript test() method, It will take the following inputs
solution
/^(?=.*\d)^(?=.*[!#$%'*+\-/=?^_{}|~])(?=.*[A-Z])(?=.*[a-z])\S{8,15}$/gm
May contains any letter except space
At least 8 characters long but not more the 15 character
Take at least one uppercase and one lowercase letter
Take at least one numeric and one special character
But I am not able to perform below task with (period, dot, fullStop)
(dot, period, full stop) provided that it is not the first or last character, and provided also that it does not appear two or more times consecutively.
Can anyone one help me to sort out this problem, Thanks in advance

You may move the \S{8,15} part with the $ anchor to the positive lookahead and place it as the first condition (to fail the whole string if it has spaces, or the length is less than 8 or more than 15) and replace that pattern with [^.]+(?:\.[^.]+)* consuming subpattern.
/^(?=\S{8,15}$)(?=.*\d)(?=.*[!#$%'*+\/=?^_{}|~-])(?=.*[A-Z])(?=.*[a-z])[^.]+(?:\.[^.]+)*$/
See the regex demo
Details:
^ - start of string
(?=\S{8,15}$) - the first condition that requires the string to have no whitespaces and be of 8 to 15 chars in length
(?=.*\d) - there must be a digit after any 0+ chars
(?=.*[!#$%'*+\/=?^_{}|~-]) - there must be one symbol from the defined set after any 0+ chars
(?=.*[A-Z]) - an uppercase ASCII letter is required
(?=.*[a-z]) - a lowercase ASCII letter is required
[^.]+(?:\.[^.]+)* - 1+ chars other than ., followed with 0 or more sequences of a . followed with 1 or more chars other than a dot (note that we do not have to add \s into these 2 negated character classes as the first lookahead already prevalidated the whole string, together with its length)
$ - end of string.

Related

Regular expression to validate a string starts with a char and contains allowed chars

I need help with my regular expression written in javascript.
I have tried using the regularExpression generator online, and the best i can come up with is the this:
^[a-z.-]{0,50}$
The expression must validate the following
String first char MUST start with a-z (no alpha)
String can contain any char in range a-z (no alpha), 0-9 and the characters dash "-" and dot "."
String can be of max length 50 chars
Examples of success strings
username1
username.lastname
username-anotherstring1
this.is.also.ok
No good strings
1badusername
.verbad
-bad
also very bad has spaces
// Thanks
Almost (assuming "no alpha" means no uppercase letters)
https://regex101.com/r/O9hvLP/3
^[a-z]{1}[a-z0-9\.-]{0,49}$
The {1} is optional, I put it there for descriptive reasons
I think this should cover what you want
^[a-z][a-z0-9.-]{0,49}$
That is starts a-z but then has 0-49 of a-z, 0-9 or .-
Live example: https://regexr.com/5k8eu
Edit: Not sure if you intended to allow upper and lowercase, but if you did both character classes could add A-Z as well!
If the . and - can not be at the end, and there can not be consecutive ones, another option could be:
^[a-z](?=[a-z0-9.-]{0,49}$)[a-z0-9]*(?:[.-][a-z0-9]+)*$
Explanation
^ Start of string
[a-z] Match a single char a-z
(?=[a-z0-9.-]{0,49}$) Assert 0-49 chars to the right to the end of string
[a-z0-9]* Match optional chars a-z0-9
(?:[.-][a-z0-9]+)* Optionally match either . or - and 1+ times a char a-z0-9
$ End of string
Regex demo

Regex for String in React

How shall we write a regular expression to validate a string that should have a length of a minimum of 1 character and a maximum of 50 characters, have both upper case and lower case, alphanumeric, include space, and have mostly used special characters like #,._-&$#? The first character should be either alphabet or number then the rest can be as mentioned above.
*If it is only one character then it should be an alphanumeric one
I have tried a regex with my limited knowledge which looks like
^[a-zA-z]*[a-zA-Z\d\-_#&$%#\s]{1,50}$
But I am not able to match the string if there is only one character given, can anyone guide me to fix this
You can use
/^(?=[\p{L}0-9])[\p{L}\p{N}_#,.&$%#\s-]{1,50}$/u
See the regex demo
Details
^ - start of string
(?=[\p{L}0-9]) - the first char must be a Unicode letter (\p{L}) or an ASCII digit
[\p{L}\p{N}_#,.&$%#\s-]{1,50} - one to fifty
\p{L} - any Unicode letter
\p{N} - any Unicode digit
_#,.&$%#- - any of these chars
\s - any whitespace
$ - end of string.

Regex not to allow to consecutive dot characters and more

I am trying to make a JavaScript Regex which satisfies the following conditions
a-z are possible
0-9 are possible
dash, underscore, apostrophe, period are possible
ampersand, bracket, comma, plus are not possible
consecutive periods are not possible
period cannot be located in the start and the end
max 64 characters
Till now, I have come to following regex
^[^.][a-zA-Z0-9-_\.']+[^.]$
However, this allows consecutive dot characters in the middle and does not check for length.
Could anyone guide me how to add these 2 conditions?
You can use this regex
^(?!^[.])(?!.*[.]$)(?!.*[.]{2})[\w.'-]{1,64}$
Regex Breakdown
^ #Start of string
(?!^[.]) #Dot should not be in start
(?!.*[.]$) #Dot should not be in start
(?!.*[.]{2}) #No consecutive two dots
[\w.'-]{1,64} #Match with the character set at least one times and at most 64 times.
$ #End of string
Correction in your regex
- shouldn't be in between of character class. It denotes range. Avoid using it in between
[a-zA-Z0-9_] is equivalent to \w
Here is a pattern which seems to work:
^(?!.*\.\.)[a-zA-Z0-9_'-](?:[a-zA-Z0-9_'.-]{0,62}[a-zA-Z0-9_'-])?$
Demo
Here is an explanation of the regex pattern:
^ from the start of the string
(?!.*\.\.) assert that two consecutive dots do not appear anywhere
[a-zA-Z0-9_'-] match an initial character (not dot)
(?: do not capture
[a-zA-Z0-9_'.-]{0,62} match to 62 characters, including dot
[a-zA-Z0-9_'-] ending with a character, excluding dot
)? zero or one time
$ end of the string
Here comes my idea. Used \w (short for word character).
^(?!.{65})[\w'-]+(?:\.[\w'-]+)*$
^ at start (?!.{65}) look ahead for not more than 64 characters
followed by [\w'-]+ one or more of [a-zA-Z0-9_'-]
followed by (?:\.?[\w'-]+)* any amount of non capturing group containing a period . followed by one or more [a-zA-Z0-9_'-] until $ end
And the demo at regex101 for trying

Javascript regex negation - Negate period on email regex

I want an existing email regex to fail when entering a period (".") before the #.
This is the regex I have right now:
^[a-zA-Z]+[a-zA-Z0-9.]+#domain.com$
These should pass:
test.a#domain.com
a.test#domain.com
But these shouldn't:
.test#domain.com
test.#domain.com
The first case starting with period is handled but second case is not.
This should work without requiring two or more characters before the # sign.
^[a-zA-Z][a-zA-Z0-9]*(?:\.+[a-zA-Z0-9]+)*#domain\.com$
Here's how it breaks down:
^ Make sure we start at the beginning of the string
[a-zA-Z] First character needs to be a letter
[a-zA-Z0-9]* ...possibly followed by any number of letters or numbers.
(?: Start a non-capturing group
\.+ Match any periods...
[a-zA-Z0-9]+ ...followed by at least one letter or number
)* The whole group can appear zero or more times, to
offset the + quantifiers inside. Otherwise the
period would be required
#domain\.com$ Match the rest of the string. At this point, the
only periods we've allowed are followed by at
least one number or letter
I would try:
^[a-zA-Z]+[a-zA-Z0-9.]*[a-zA-Z0-9]+#domain.com$
Try this regex: ^[\w.+-]*[^\W.]#domain\.com$.
[\w.+-]* matches any number of alphanumerical characters, +, - and .
[^\W.] matches any character that is not a non-alphanumerical character or a . (which means any accepted character but .)
#domain\.com matches the rest of the email, change the domain as you wish or use #\w\.\w+ for matching most domains. (matching all domains is more complex, see more complete examples of email matching regex here)

Javascript Password compliance

I need the password to fulfill these requirements
Password must contain at least 8 word characters
Must have at least 1 numeric digit e.g. 3
Must have at least 2 uppercase characters but not in one consecutive sequence
It doesn't seems to work with this
var pos = myPass.value.search(/^([\w.-]{8,})(?=.*\d)((.*?[A-Z]){2,})$/);
No.3 is the hardest.
You are mixing consuming and non-consuming patterns adding limiting quantifiers to the consuming patterns that match a sequence, while you need to just check if a string matches some restrictive patterns or not. To add those restrictions you need lookaheads. (?=.*\d) is a correct part of your regex, other aren't.
Also, a RegExp#test() is a better method to check if a string matches or not.
Use
/^(?=\D*\d)(?=(?:(?:^|[^A-Z]+)[A-Z]){2}).{8,}$/.test(my‌​Pass.value)
See the regex demo
Or, to allow only letters, digits, underscores, dots and hyphens in the password:
/^(?=\D*\d)(?=(?:(?:^|[^A-Z]+)[A-Z]){2})[\w.-]{8,}$/.test(my‌​Pass.value)
^^^^^^
Details:
^ - start of string
(?=\D*\d) - after 0+ non-digits (\D*) at the start of the string, there must be a digit (\d) (note that after this lookahead execution, the regex index is still at the beginning of the string)
(?=(?:(?:^|[^A-Z]+)[A-Z]){2}) - there must be 2 sequences ((?:...){2}) of:
(?:^|[^A-Z]+) - start of string or one or more chars other than uppercase letters
[A-Z] - an uppercase letter.
.{8,} - any 8 or more chars other than those used in a linebreak sequence
OR
[\w.-]{8,} - 8 or more ASCII letters and digits, underscores, dots or hyphens
$ - end of string.

Categories

Resources