My code:
preg_match("/^(?=.*[a-zA-Z].*[a-zA-Z].*[a-zA-Z])[0-9A-Za-z!@#$%^&_ -\']{3,48}$/", $hostname)
I want the site to accept user names like: John’s Pub. At the moment this kind of works because it allows for single quote (‘) but it also accepts ().
I have tried single quote on it’s own, with 1 and 2 but none of this works.
2
Answers
You have several problems:
[A-Za-z]
-
in a[]
. block it means range! Escape it-
.\'
it means aand
'
which leads to nothing right!w
instead of0-9A-Za-z
. It’s simpler.Try this:
If you want to allow
character too, use this:
If you want to allow
()
characters too, use this:All Combined:
To be specific to your problem you can try this:
Where:
A
means Start of Stringw
means Any word character (letter, number, underscore)*
means 0 or more than 0 time(of prefix of*
, here prefix isw
)'
means allow character'
similarly(
and)
is for defining allowed character[]
is group operators
is for whitespacez
is for end of StringHope this will help
Edit:
You can try your custom regex here as well