password regex for PHP with 3 possible conditions - php

Is it even possible to write a regex for PHP with the following conditions - in a one line regex?
The password must contain any ONE of the following:
Contain a letter and a digit.
Contain a letter and a special character
Contain a digit and a special character.

This one must contain: One number, one uppercase and one lowercase letter:
^\w*(?=\w*\d)(?=\w*[a-z])(?=\w*[A-Z])\w*$

This regex I came up with is very close:
(?=.*[a-zA-Z\d])(?=.*[a-zA-Z\W])(?=.*[\d\W]).*$
It succeeds in requiring any of the 3 combinations:
Must contain a letter and a digit.
OR
Must contain a letter and a special character
OR
Must contain a digit and a special character.
The only problem left is that whitespace can be substituted for any part of the above combos, and the password will work. I want whitespace to be allowed, but for one of the 3 requirements to still be met for the password to pass, whitespace or not.

Related

Username validation with regular expression in php

I am trying to validate a USERNAME in PHP using Regular Expression. But I am failed. My pattern is: /[^a-z0-9_]/
Rules:
username must start number or small letters
username support number, small letters and _
the end of username character is not be _
The following pattern will work: ^[a-z0-9][a-z0-9_]*[a-z0-9]$
^[a-z0-9]: first character may not be an underscore
[a-z0-9_]*: all the others may be anything...
[a-z0-9]$: ...except the last one which can't be an underscore.
Assuming the minimum number of characters is two (if less than three, the underscore never would be permitted), here is the correct pattern:
^[a-z0-9][a-z0-9_]*[a-z0-9]$
First character is small letters or digit (there can be more of them, that's why the + sign. Then any amount (including zero) of characters including underscore. At the end again, at least one letter or digit.
You can test it here:
https://regex101.com/r/C1zPfu/1
You do not want username of two letters or username that has unlimited characters, do you? Consider this solution if you need to limit the number of characters in your username under your situation:
/^[a-z0-9][a-z0-9_]{2,28}[a-z0-9]$/
https://www.tinywebhut.com/regex/4
The part [a-z0-9] exactly matches one character which can be only small letter or number. The middle part [a-z0-9_]{2,28} matches any small letter or number up to 2 or 28 characters including underscore. The final part [a-z0-9] exactly matches one character which can be only small letter or number. Therefore, this regular expression matches username that has at least 4 characters and 30 characters at the most. If you change your mind and want to include both small and capital letters, you'll have to add a modifier i:
/^[a-z0-9][a-z0-9_]{2,28}[a-z0-9]$/i
https://www.tinywebhut.com/regex/5

Add min char and a way to find words with first letter capitalized to a regex

Hi guys have the following regex:
/([A-Z][\w-]*(\s+[A-Z][\w-]*)+)/
I've tried in different way, but i'm not a pro with regex..so, this is what want to do:
Add a rule that match only 3+ characters words.
Add a rule that can match name like "Institute of Technology" (so, three words with a lowercase word between the first and the last)
Can you help me to do that? (I should do different regex, am i right?)
In order to help you to understand, this is what you have:
[A-Z]: one character in the class A-Z
[\w-]*: a concatenation of zero or more word character or hypens
(...)+: one or more:
\s+: at least one space
[A-Z]: one character in the class A-Z
[\w-]*: a concatenation of zero or more word character or hypens
This is what you want:
[A-Z]: a capital letter
[\w-]*: a concatenation of zero or more word character or hypens
\s+: at least one space
[a-z]: a lower-case letter
[\w-]*: a concatenation of zero or more word character or hypens
\s+: at least one space
[A-Z]: a capital letter
[\w-]*: a concatenation of zero or more word character or hypens
That is:
[A-Z][\w-]*\s+[a-z][\w-]*\s+[A-Z][\w-]*
You may want to do some small changes. I think you can do them by your own.
A rule that matches only 3+ characters word is \w{3,}. If you want to capitalize the first character use [A-Z]\w{2,}.
(\w\w\w+)|(\w+ [a-z]+ \w+) - This code searches for a word consisting of at least 3 letters OR a word with at least 1 sign, space, small letters, 1+ signs. You can switch \w with [A-Z] if necessary.
If your 3 word phrase has to have 2 words with capital letters, change the second brackets to ([A-Z]\w* [a-z]+ [A-Z]\w*). Try it here: https://regex101.com/r/E3IPTj/1
Not sure on the scope of your limitations but a few 'building blocks' might help. Also id suggest just starting at the beginning I don't know any recent websites that handle learning regex well but when I started I used the following http://www.regular-expressions.info/tutorial.html (It's been many years, and the website does reflect its age so to speak)
However onto your regex:
Following your example: Institute of Technology
You need to know just a few things, character sets (and how to use matching length) and the space.
Character sets match one length (by default) and are done like for example [abc] that will match a, b, or c, and also supports character ranges (a-z)/grouped (eg. \d all digits).
The match length can be changed by using the:
+ - one or more (examples: a+, [abc]+, \d+)
* - zero or more (examples: a*, [abc]*)
And this one you might want but thats up to you
{min, max} - specific range, eg. b{3,5} will match 3-5 joined 'b' characters (bbb, bbbb, bbbbb) max can be omitted `{min,} to have at least min chars but no max
Spaces are done using "" (a space), (\s matches any whitespace character (equal to [\r\n\t\f\v ]) (spaces, tabs, newlines, ...)
In your example its a matter of case sensitive or not if not case sensitive we can use a simple [A-Za-z]+ to match upper and lowercase a-z of at least one length, together with the space we get something along the lines of
/[A-Za-z]+ [A-Za-z]+ [A-Za-z]+/
It's that simple. For case insensitive matching there is also an option flag, we can use i which will result in
/[a-z]+ [a-z]+ [a-z]+/i
If you do want to have case sensitive matching you will need to separate them how you like:
/[A-Z][a-z]* [a-z]+ [A-Z][a-z]*/ // (*A a A*)
As a small change I've also changed + into * so the lowercase part is not required, again up to you.
Also note that to match the beginning of a string your required to use ^ and to match the end of a string use $ the above examples will match any segment, not the whole input eg: qhg8Institute of Technology8tghagus would work
So final result:
/^[A-Z][a-z]* [a-z]+ [A-Z][a-z]*$/ // case sensitive (Aa a Aa)
/^[a-z]+ [a-z]+ [a-z]+$/i // case insensitive
Obviously there is lots more to learn that can be used to expand/ optimize this but regex are so customizable its really up to the person needing them to specify his/ her limitations/ requirements.
As a side note I noticed people using \w for word chars, but this also includes digits, _, and special language letters like à, ü, etc. Again up to you what to do with this.

Regex for password must contain at least 8 characters, at least 1 number, letters and special characters

I need a regular expression which should have at least one numeric character, both uper and lower case letters allowed, special characters also allowed I am using this expression
/^.*(?=.{6,10})(?=.*\d)(?=.*[a-zA-Z]).*$
but it is not valid for max characters 10.
Seems like you want something like this,
^(?=.*\d)(?=.*?[a-zA-Z])(?=.*?[\W_]).{6,10}$
The above regex would allow 6 to 10 characters only. And it also checks for at-least one digit, upper or lowercase letter and at-least one special character (characters other than letters and numbers).
The following regular expression will limit your length and allow special characters.
^(?=.*\d)(?=.*[a-zA-Z]).{6,10}$

Regex for user login verification

I am trying to use regex to check if a user name is considered valid. For a user name to be valid it must be between 2 and 10 characters long. It must start with a lower case letter and it must be made up of only lowercase letters, upper case letters or digits.
Here is what I have so far
return preg_match("/^[a-z]([a-zA-Z0-9]{2,10})$/", $name);
For consistency, your capture expression should include your whole username (in your case you are skipping the first letter). You've also noticed you only need to use {1,9} for the second half:
^([a-z]{1}[a-zA-Z\d]{1,9})$
this is matching one lowercase letter to begin with, followed by between 1 and 9 a-zA-Z0-9s afterwards, and capturing the whole username.
Modifiers (if required): mg (multi-line, match-all [for when testing with a block of multiple lines])
Demo: http://regex101.com/r/qW6kN5

Regular expression for validating a username?

I'm still kinda new to using Regular Expressions, so here's my plight. I have some rules for acceptable usernames and I'm trying to make an expression for them.
Here they are:
1-15 Characters
a-z, A-Z, 0-9, and spaces are acceptable
Must begin with a-z or A-Z
Cannot end in a space
Cannot contain two spaces in a row
This is as far as I've gotten with it.
/^[a-zA-Z]{1}([a-zA-Z0-9]|\s(?!\s)){0,14}[^\s]$/
It works, for the most part, but doesn't match a single character such as "a".
Can anyone help me out here? I'm using PCRE in PHP if that makes any difference.
Try this:
/^(?=.{1,15}$)[a-zA-Z][a-zA-Z0-9]*(?: [a-zA-Z0-9]+)*$/
The look-ahead assertion (?=.{1,15}$) checks the length and the rest checks the structure:
[a-zA-Z] ensures that the first character is an alphabetic character;
[a-zA-Z0-9]* allows any number of following alphanumeric characters;
(?: [a-zA-Z0-9]+)* allows any number of sequences of a single space (not \s that allows any whitespace character) that must be followed by at least one alphanumeric character (see PCRE subpatterns for the syntax of (?:…)).
You could also remove the look-ahead assertion and check the length with strlen.
make everything after your first character optional
^[a-zA-Z]?([a-zA-Z0-9]|\s(?!\s)){0,14}[^\s]$
The main problem of your regexp is that it needs at least two characters two have a match :
one for the [a-zA-Z]{1} part
one for the [^\s] part
Beside this problem, I see some parts of your regexp that could be improved :
The [^\s] class will match any character, except spaces : a dot or semi-colon will be accepted, try to use the [a-zA-Z0-9] class here to ensure the character is a correct one.
You can delete the {1} part at the beginning, as the regexp will match exactly one character by default

Categories