I trying to create a Regular Expression for a password that can accept only
digits and letters (a-zA-Z).
In the password at least one digit must to be, and at least one letter. All the text between 10-12 symbols.
I created the next REGEX :
/^(?=.*[A-Za-z])(?=.*[0-9]).{10,12}$/
The problem is that any signs like $,%me,#,space also acceptable.
How can I add a rule saying any others signs accepts letters,digits are not acatbale?
Instead of using ., which is any character, use [A-Za-z0-9]:
/^(?=.*[A-Za-z])(?=.*[0-9])[A-Za-z0-9]{10,12}$/
The lookaheads will be satisfied as long as there is a single alphabetic and a single numeric character in the 10-12 list of characters, but . allows any of the other 8-10 characters be anything.
However, I would suggest you not do this because requirements on passwords are not a good thing. You should let people enter whatever password they want.
Try with this:
/^[a-zA-Z0-9]*$/
and with limit:
/^[a-zA-Z0-9]{10,12}$/
Related
I have added a regex which will check for least 8 characters, at least one number, one uppercase letter, one lowercase letter and one special character
This is the regex Used
'(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[$#$!%*?&])[A-Za-zd$#$!%*?&].{8,}'
The above regex works fine for most of the scenarios but when I use
1oB!gb0s5
or
Pass#123
It fails. Can anyone tell me the issue here.
Here is the portion of your regex which actually consumes the input:
[A-Za-zd$#$!%*?&].{8,}
This means that the password must start with one of the characters in the above character class. It also means that a valid password must have nine or more characters, because the class counts for one, and {8,} means 8 or more. So the following would fail because it does not begin with any such character:
1oB!gb0s5
The second example you gave fails for a different reason, because it only has 8 characters:
Pass#123
I don't know exactly what logic you want here. If you just want to ensure that a password has a lowercase, uppercase, number, and special character, then maybe you can remove the leading character class and just stick with the lookaheads:
(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[$#$!%*?&]).{8,}
Here is a demo which shows that your two example passwords would pass using the above pattern.
Demo
I have seen answers for strong password checks but I have an additional requirement for the order in which the characters appear.
The password should contain at least:
One upper case letter.
One lower case letter.
One number.
One special character.
The order being:
Should start with upper case and lower case letters.
Followed by number and/or alphabetical characters.
In the end, should be a special character.
e.g.
Xyz1325# is valid.
aBcd123xYz# is also valid.
#Xyz1234 is invalid.
1234Xyz# is invalid.
Xyz# is invalid.
Introducing a specific order of characters in a password makes it relatively more predictable, hence losing the strength of it, and I'll suggest you to get away with that restriction. Nevertheless, you can use this regex that will meet your needs,
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[~!##$%^&*])[a-zA-Z][a-zA-Z\d]*[~!##$%^&*?<>]*$
Explanation:
^ --> Start of string
(?=.*[a-z]) --> Ensures the password has at least one lowercase letter.
(?=.*[A-Z]) --> Ensures the password has at least one uppercase letter.
(?=.*\d) --> Ensures the password has at least one digit.
(?=.*[~!##$%^&*]) --> Ensures the password has at least one special character from this set. You can put more characters inside that you want to treat as special.
Now comes the part for ensuring the order. As you said it should start with an alphabet, hence we need the first character as,
[a-zA-Z]
Then following it can be alphabet or numbers hence you can use,
[a-zA-Z\d]*
And finally you want special characters, and by your this statement "In the end, should be a special character." I assume you do not want to restrict it to just one single special character, hence at the end of regex it should be this,
[~!##$%^&*?<>]*
which can match one or more special characters. If you really meant just one special character then just make it [~!##$%^&*?<>]
And finally end it with $ to stop matching the string.
Live Demo
Hope this works for you. Or else, let me know for any other queries.
Edit
Bonus:
If you want to check length as well you can do so using the following:
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[~!##$%^&*])(?=.{6,18}$)[a-zA-Z][a-zA-Z\d]*[~!##$%^&*?<>]*$
The additional (?=.{6,18}$) is to ensure that your regex has length between 6 to 18.
#Pushpesh,
Please correct if wrong.
The regex I believe you are looking for is this: https://regex101.com/r/nO2DxE/2
Explanation:
These groups (?=.*[A-Z].*) (?=.*[0-9].*) (?=.*[a-z].*) make sure your string contains at least one uppercase letter, one lowercase letter and one digit. The rest of the regex checks that the order you described is respected.
Overall, the regex is:
(?=.*[A-Z].*)(?=.*[0-9].*)(?=.*[a-z].*)^[a-zA-z][a-zA-Z0-9]*[#!#+-]$
I'm trying to modify my regex which requires user to type at least one letter and one digit which looks like this :
new RegExp('^(?=.*[a-z])(?=.*[0-9]).+$')
And I want to prevent user from using certain words like email address part before #.
So let's assume the email address is example#example.com
I want to force user to use a string that doesn't contain example in it (any part of the string)
This is what I have so far:
\b(?:(?!example)\w)+\b
But it doesn't really force the user to use at least one character and one digit.
When I'm trying to restric it I'm ending up with this:
\b(?:(?!example).*[a-z].*[0-9])+\b
But now the strings must follow the order of example then a [a-z] and then [0-9]
Any help greatly appreciated
Thanks!
In your sample the negative lookahead disallows example only at start of the string. Just add .*? as joker to disallow the word anywhere in the string and use word boundaries \b if needed.
/^(?!.*?example)(?=\D*\d)[^a-z]*[a-z].*$/i
(?!.*?example) first lookahead disallows example anywhere in the line
(?=\D*\d) second lookahead requires a digit after any amount of \D non-digits.
[^a-z]*[a-z] matches any amount of non-alphetic characters until an alphabetic.
See demo at regex101
Actually you just need two lookaheads for being independent of condition. One for the required digit and one for the word. The requirement of alphabetic can be done inside the pattern.
Lookarounds are zero-length assertions triggered at a certain position.
I'm trying to create a regex to test passwords against. My current one checks for the following:
One Uppercase Letter
One Lowercase Letter
One number
Currently, the user can't enter special characters, however I'm trying to add that as an optional check (so Testing1 and Testing1! should both match). I've tried:
^(?=.*[A-Za-z])(?=.*\d)(?=.*[$#$!%*#?&])(A-Za-z\d[$#$!%*#?&]?){8,}$
But it doesn't catch it. I have a feeling my special character set is in the wrong place, but I'm not sure where to place it.
Where do I add my list of special characters as optional checks?
There's many ways that you can set up your regex, such as creating a whitelist, or a blacklist, for types of characters. This one in particular creates a whitelist for characters that can be used which seems to be what you are looking for.
^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])[A-Za-z0-9$#$!%*#?&]{8,}$
Regex Breakdown:
^ // Assert position at start of the line
(?=.*[A-Z]) // First positive lookahead, makes sure a capital character exists
(?=.*[a-z]) // Make sure a lowercase character exists
(?=.*[0-9]) // Make sure a number exists
[A-Za-z0-9$#$!%*#?&] // All of the possible characters that can be typed
{8,} // 8 to infinity characters
$ // Assert position at end of line
Since you say that you want special characters as optional, they are just placed in the possible characters that can be typed, but they are not validated by any positive lookaheads.
See this regex in action on regex101. Keep in mind, the modifiers gm are there to validate across lines in this example and should probably be removed in your use case.
Of course you may have reasons for the "whitelist" approach, but a more common approach, and one you may want to look into trying sometime, is to allow almost anything (blacklist), and then validate that a certain criteria is met.
i'm currently using this regex
"/^([0-9\(\)\/\+ \-]*)$/",
which is fine,but the problem is i am also using a masking script,
which produces this line automatically,
(___) ___-____
and it messes up my validation, what regex code can allow me to verify only this type of input from the use
(999) 999-9999
and also not accept a "blank" input field from user when entered. any length is fine, as long as it only accepts this inputs that i mentioned above.
This should work:
^\(\d{3}\)\s{0,1}\d{3}-\d{3}$
Breaking this regexp:
\(\d{3}\) matches only three numbers between brackets.
\s{0,1} matches only 0 or 1 space.
\d{3}-\d{3} matches only three numbers followed by '-' and then three other numbers.
First, when asking about regular expressions, you should always say which language or tool you are using because that affects what features are available and which characters need to be quoted with backslash. I'll assume you're asking about JavaScript based on your question's tags.
You say any length is fine. I shall take that to mean that each sequence of consecutive digits can contain any number of digits from one to infinity. I shall assume there's exactly one space and exactly one dash. On that basis, your RE is:
/^\(\d+\) \d+-\d+$/
If, as is more likely, you want to limit the lengths of the digit sequences, you would say something like:
/^\(\d{3,4}\) \d{3}-\d{1,10}$/
(three or four digits, exactly three digits, one to ten digits).
I have omitted any capturing parentheses (...) , which are a bit redundant if you're capturing the whole string ^(....)$ .
Here's a concise summary of JavaScript regex syntax:
http://www.regextester.com/jssyntax.html
Formatting and validation are two very different things. If you try to mix them, you will fail.
That being said, before performing validation you should strip all formatting characters from your string, then validate the content.
// remove everything that isn't a digit
var strippedNumber = value.replace(/\D/g, '');
if (strippedNumber.length === 10) {
// valid phone number
}