Custom regular expression for password with one special character - php

So I need a regular expression that can provide the following rules
8 character long minimum,
at least one UPPERCASE character
1 special character
I have something like this right now:
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).{4,8}$
But I also need the special character, and I don't know how to do that, if anyone has a suggestion

You can add the special characters you wish to allow. The match you have currently is matches between 4 and 8 characters, the one below minimum 8 maximum 20
((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[##$%]).{8,20})

Related

How to construct a regex expression for username validation? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I'm working on constructing a PHP regex expression for a username validation that has the following constraints:
-must be 10-16 characters long, with a combo of alphabetic, numeric, and atleast one special character (*&^-_$)
-can't start with a numeric or special character
CORRECTION: the last SIX digits must be a month/date birthday (MMYYYY format). In order to further validate the username, the month/date must show the username is over 18 - if not, the username will not validate. Thank you in advance for any assistance! I've been stuck on this for a while.
Solution
You can do this with the following regex:
/(?=^.{10,16}$)(?=.+?[*&^_$-])[a-z].+?[01]\d{3}$/i
Here's a demo with some unit tests.
Explanation
/ delimiter
(?=^.{10,16}$) ensures there are 10-16 characters, start to finish
(?= starts a lookahead group
^ start of the string
.{10,16} ten to sixteen characters
$ end of the string
) ends the lookahead group
(?=.+?[*&^_$-]) ensures there is at least one special character in the set *&^_$-, and it's not first
(?= starts a lookahead group
.+? one or more characters, non-greedy
[*&^_$-] any character in the set *&^_$- (note the order; you must put - first or last, or escape it as \-)
) ends the lookahead group
[a-z] start with a letter
.+? match any characters in a non-greedy fashion, giving back as needed
[01]\d{3} match a 0 or 1 then 3 more digits
$ match the end of the string
/ closing delimiter
i make the match case-insensitive
Some Notes on Regex Construction
Note that there are multiple valid ways to do this. For pure efficiency, the solution above could be simplified somewhat to cut out a few steps for the processor.
But for readability, I like to go with something like the above. It's clear what each block, character set, or group does, which makes it readable and maintainable.
Something like /^[a-z](?=.*?[*&^_$-])[a-z0-9*&^_$-]{5,11}[01]\d{3}$ is hard to read and understand. What if you want to allow a 17 character username? You have to do a bunch of math to determine that you should change {5,11} to {5,12}. Or if you decide to allow the character #, you'd have to add it in two places (which, by the way, means that the regex already violates the DRY principle).
Bonus: Why Your Attempt Failed
You said in a comment that you tried this:
(?=^.{10,16}$)^[a-z][\d]*[_$^&*]?[a-z0-9]+
The first part, (?=^.{10,16}$), is fine. So is ^[a-z].
But [\d]* only matches zero or more digits; it wouldn't match a letter or special character. So, for example, a&a... would fail.
And [_$^&*]? only matches zero or one special characters. It would allow a username with no special characters to pass, but would fail one with 2 special characters.
[a-z0-9]+ only matches those characters, and you omit your last-four-characters-must-be-digits requirement.
You might find the explanation on regex101.com of your regex helpful. (Note: I have no affiliation with that site.)
You can use this regex :
^[a-zA-Z](?=.*[*&^_$-])[\w*&^$-]{5,11}[01]\d{3}$
Regex Breakup:
^ # Line start
[a-zA-Z] # # match an alhpabet
(?=.*[*&^_$-]) # lookahead to ensure there is at least one special char
[\w*&^$-]{5,11} # match 5 to 11 of allowed chars
[01]\d{3} # match digits 0/1 followed by 3 digits
$ # Line end
I used quantifier {5,11} because one char is matches at start and 4 are being matched in the end thus taking out 5 positions from desired {10,16} lengths.

Regular Expression for starting woth specfic number

How to write regular expression for following conditions:
Should have only numbers
Must be 8 digits long
Must start with 8 or 9 or 6
So for I can do only for first two conditions. I am not sure how to do the third conditions
My code is
if (!preg_match('/^[0-9]{8}$/', $number))
You're almost there. Simply remove the first number from the character class and validate it at the start of your pattern...
/^(8|9|6)\d{7}$/
FYI - \d is the escape sequence for digits. I suppose you could also use this
/^[896]\d{7}$/
as it means about the same thing when you're only watching for a single character at the start.

Regex to change to must include at least a number and letter?

My regex at the minute is like this
'/[a-z0-9]{40}/i'
Which will match any string with no spaces that contains letters and/or numbers.
How can I change it so that it must at least include at least one number and at least one alphabet character so that if the string was all numbers or all letters it would not be matched?
Thanks
/([:alpha:].*[:digit:]|[:digit:].*[:alpha:])/
This requires a number to follow a letter, or a letter to follow a number.
From your original regex, it appears that you want to enforce a requirement for 40 characters total. For that, try:
/^(.*[:alpha:].*[:digit:].*|.*[:digit:].*[:alpha:].*){40}$/
Note the extra .*'s. As long as there's one alpha and one digit, the other characters can be anything. As long as there are 40 of them.
If you want to avoid matching whitespace, replace each .* with [^[:space:]]*.

Regex help to match more than one letter

I am using the following regex to match an account number. When we originally put this regex together, the rule was that an account number would only ever begin with a single letter. That has since changed and I have an account number that has 3 letters at the beginning of the string.
I'd like to have a regex that will match a minimum of 1 letter and a maximum of 3 letters at the beginning of the string. The last issue is the length of the string. It can be as long as 9 characters and a minimum of 3.
Here is what I am currently using.
'/^([A-Za-z]{1})([0-9]{7})$/'
Is there a way to match all of this?
You want:
^[A-Za-z]([A-Za-z]{2}|[A-Za-z][0-9]|[0-9]{2})[0-9]{0,6}$
The initial [A-Za-z] ensures that it starts with a letter, the second bit ([A-Za-z]{2}|[A-Za-z][0-9]|[0-9]{2}) ensures that it's at least three characters long and consists of between one and three letters at the start, and the final bit [0-9]{0,6} allows you to go up to 9 characters in total.
Further explaining:
^ Start of string/line anchor.
[A-Za-z] First character must be alpha.
( [A-Za-z]{2} Second/third character are either alpha/alpha,
|[A-Za-z][0-9] alpha/digit,
|[0-9]{2} or digit/digit
) (also guarantees minimum length of three).
[0-9]{0,6} Then up to six digits (to give length of 3 thru 9).
$ End of string/line marker.
Try this:
'/^([A-Za-z]{1,3})([0-9]{0,6})$/'
That will give you from 1 to 3 letters and from 3 to 9 total characters.

Regular expression to match letters, numbers, and certain symbols

I need to validate a username in PHP. It can be:
Letters (upper and lower case)
Numbers
Any of these symbols :.,?!#
Up to 15 characters OR 16 if the last character is one of the following #$^ (it can also be 15 or less with one of these 3 characters at the end only)
How do I do this?
Start with this:
/^[a-zA-Z0-9:.,?!#]{3,15}[#$^]?$/
then refine it to your needs. Try to see if you need escaping of the special char, but you should get the idea.
This means: from a to z, from A to Z, from 0 to 9 and :.,?!# repeated from 3 to 15 times, optionally followed by one among #$^

Categories