Username validation with regular expression in php - 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

Related

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.

php preg_match only numbers, letters and dot

I have been searching for 2 hours now and I still don't get it.
I need to evaluate the input of the account name. That can ONLY contain numbers (0-9), letters (a-z and A-Z) and the dot (.).
Everything else is forbidden. So, no underscore (_), plus (+) and so on.
Valid accounts should look like, e.g.:
john.green
luci.mayer89
admin
I tried many preg_match/regex examples but I don't get it working. Whenever I do echo preg_match(...) I get 1 as true.
$accountname = "+_#luke123*";
echo preg_match("/[a-z0-9.]/i", $accountname);
//--> gives back 1 as true
In addition, it would be great to control that the account name starts with at least 2 letters or numbers and ends with at least 1 letter or number - but I am far, far away from that.
You need to use anchors and a quantifier:
echo preg_match("/^[a-z0-9.]+$/i", $accountname);
Your string +_#luke123* contains a letter and a number, thus there is a match. If we tell the engine to only match the whole string from beginning (^) to end ($), we'll make sure this will not match. + ensures we capture not just 1, but all characters.
See this demo, now there is no match!
EDIT:
Since you also need to check these conditions:
string must start with 2 or more letters or numbers and end with 1 or
more letters or numbers
I can suggest this ^[a-z0-9]{2,}[a-z0-9.]*[a-z0-9]+$ regex (must be used with i option) that means:
Starts with 2 or more letters or numbers
then follow any number of digits, letters or periods
and ends in 1 or more letters or numbers.
Another demo

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

password regex for PHP with 3 possible conditions

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.

Categories