I was wondering if the codes below are the correct way to check for a street address, email address, password, city and url using preg_match using regular expressions?
And if not how should I fix the preg_match code?
preg_match ('/^[A-Z0-9 \'.-]{1,255}$/i', $trimmed['address']) //street address
preg_match ('/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/', $trimmed['email'] //email address
preg_match ('/^\w{4,20}$/', $trimmed['password']) //password
preg_match ('/^[A-Z \'.-]{1,255}$/i', $trimmed['city']) //city
preg_match("/^[a-zA-Z]+[:\/\/]+[A-Za-z0-9\-_]+\\.+[A-Za-z0-9\.\/%&=\?\-_]+$/i", $trimmed['url']) //url
Your street address:
^[A-Z0-9 \'.-]{1,255}$dotin the charclass, it will allow all char (except
newline). So effective your regex becomes
^.{1,255}$length of 1 and max of length 255. I
would suggest you to increase the min
length to something more than 1.
Your email regex:
^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$.in the charclass. fix that.
Your password regex:
^\w{4,20}$and can contain only alphabets(upper
and lower), digits and underscore. I would suggest you to allow
special char too..to make your
password stronger.
Your city regex:
^[A-Z \'.-]{1,255}$.in char classto allow cities of 1 char length this
is fine).
EDIT:
Since you are very new to regex, spend some time on Regular-Expressions.info