Validations php with regex
Validations php with regex
Regular expressions (shortened as "regex") are special strings representing a pattern to be matched
in a search operation. They are an important tool in a wide variety of computing applications,
from programming languages like Java and Perl, PHP, Python.
The power of regular expressions comes from its use of metacharacters, which are special
characters (or sequences of characters) used to represent something else. For instance, in a regular
expression the metacharacter ^ means "not". So, while "a" means "match lowercase a", "^a"
means "do not match lowercase a".
Note: There are different so-called "flavors" of regex — Java, Perl, and Python have slightly
different rules for regular expressions, for example. On this page, we stick to standard regex, and
you should be able to use this reference for any implementation.
Anchors and boundaries allow you to describe text in terms of where it's located. For instance,
you might want to search for a certain word, but only if it's the first word on a line. Or you might
want to look for a certain series of letters, but only if they appear at the very end of a word.
Metacharacter Example
Meaning Example Match
Sequence Expression
Character Classes
When searching for text, it's useful to be able to choose characters based solely upon their
classification. The fundamental classes of character are "word" characters (such as numbers and
letters) and "non-word" characters (such as spaces and punctuation marks).
Metacharacter Example
Meaning Example Match
Sequence Expression
\n a newline
Metacharacter Sequence Matches
\t a tab
\r a carriage return
\v a vertical tab
\f a form feed
Quantifiers
Quantifiers allow you to declare quantities of data as part of your pattern. For instance, you
might need to match exactly six spaces, or locate every numeric string that is between four and
eight digits in length.
Metacharacter Example
Meaning Example Match
Sequence Expression
Metacharacters are a powerful tool because they have special meaning, but sometimes they need
to be matched literally. For instance, you might need to search for a dollar sign ("$") as part of a
price list, or in a computer program as part of a variable name. Since the dollar sign is a
metacharacter which means "end of line" in regex, you must escape it with a backslash to use it
literally.
\\ Literal backslash \\ \
\^ Literal caret \^\{5\} ^^^^^
\$ Literal dollar sign \$5 $5
\. Literal period Yes\. Yes.
\* Literal asterisk typo\* typo*
\[ Literal open [3\[] 3, [
bracket
\] Literal close \] ]
bracket
A character set is an explicit list of the characters that may qualify for a match in a search. A
character set is indicated by enclosing a set of characters in brackets ([and]). For instance, the
character set [abz] will match any of the characters a, b, or z, or a combination of these such
as ab, za, or baz.
Ranges are a type of character set which uses a dash between characters to imply the entire
range of characters between them, as well as the beginning and end characters themselves. For
instance, the range [e-h] would match any of the characters e, f, g, or h, or any combination of
these, such as hef. The range [3-5] would match any of the digits 3, 4, or 5, or a combination of
these such as 45.
When defining a range of characters, you can figure out the exact order in which they appear by
looking at an ASCII character table.
Metacharacter Example
Meaning Example Match
Sequence Expression
Grouping
Example Example
Metacharacter Sequence Meaning
Expression Match
if ( !preg_match ("/^[a-zA-Z\s]+$/",$name)) {
$errors[] = "Name must only contain letters!";
}
or
Check Username:
$str = "";
function validate_username($str)
{
$allowed = array(".", "-", "_"); // you can add here more value, you want to allow.
if(ctype_alnum(str_replace($allowed, '', $str ))) {
return $str;
} else {
$str = "Invalid Username";
return $str;
}
}
Or
if (ctype_alnum($username)) {
// Username is valid
}
Or
Or
function is_valid_password($pwd){
// accepted password length between 5 and 20, start with character.
if (preg_match("/^[a-zA-Z][0-9a-zA-Z_!$@#^&]{5,20}$/", $pwd))
return true;
else
return false;
}
function is_valid_date($date){
//============ date format dd-mm-yyyy
if(preg_match("/^(\d{2})-(\d{2})-(\d{4})$/", $date, $sdate))
if(checkdate($sdate[2], $sdate[1], $sdate[3]))
return true;
}
The first requirement is to make our pattern start with letters. To accomplish that, we can use the
caret character (^). Which has a special meaning in Regex.
When we use the caret character at the start of a regex pattern, we are telling PHP to match only
strings that start with that letter. Hence, a pattern like the below will match strings starting with
PHP.
Similarly, we can use the dollar character ($) to enforce endings in a regex pattern. That means,
$input = "PHP is great";
preg_match('/^PHP$/', $input);//Will return false.
Clearly, the above code returns false because the input string ends with ‘great’ not PHP. To make
it work we can replace that string with ‘great’.
This is great and all. But it still lacks in answering the first requirements. Because we can’t list
all the alphabets and tell PHP to enforce that.
Instead of listing the alphabets, Regex gives us character class ranges. Ranges are awesome
when we want to specify the whole alphabet. Both lowercase and uppercase. Or to check every
number. For example:-
$input = "abc";
preg_match('/^[a-zA-Z]$/', $input);//Will return true.
The above code snippet is a great elaboration of ranges. It checks if the input is made up of
letters. Either small or capital letters. Similarly, we can apply the range, [0-9] to validate our
input for numbers in it.
Awesome. Now we are getting there. We are clearly checking if our user input starts and ends
with letters.
The next requirement is the size of the username. To check for the size we use the format, {n} in
regex. Where n is the number allowed.
Furthermore, we can specify the minimum and maximum value in a brace like this {n, m}.
$input = "abc2020";
preg_match('/^[a-zA-Z]{4, 25}$/', $input);//Will return true.
Alright, so we are at the third requirement. And it states that our userName can only contain
letters, numbers, and underscore. I think at this point we are capable of doing this without
introducing any new concept. We can use ranges like this, [0-9a-zA-Z_].
Finally, forbidding underscore at the end. For now, we will stick with a range syntax and we
have our final regex with preg_match function like this:-
function validateUserName($userName) {
if(preg_match('/^[a-zA-Z][0-9a-zA-Z_]{2,23}[0-9a-zA-Z]$/', $userName)) {
return true;
}
return false;
}
HTML5 forms support different input types. And one of them is the number type. You can use it
to force users to put in only numbers.
The problem is when you want to let your users format the phone number. For example, by
letting them put dash character. In those cases, we can use the character classes and range
limiters we saw above to validated formatted US and Ethiopian phone numbers like this:-
$phoneNumber = "0911-223344";
preg_match('/[0-9]{4}-[0-9]{6}/', $phoneNumber);//Simple regex to validate ethiopian phone
number
preg_match("/[0-9]{3}-[0-9]{3}-[0-9]{4}/", $phoneNumber); // Simple regex to validate US
phone number
I can simplify this pattern by replacing the character class [0-9] with a simple \d. Which simply
indicates any digits.
if (strlen($pwd) < 8) {
$errors[] = "Password too short!";
}
if (!preg_match("/[0-9]+/", $pwd)) {
$errors[] = "Password must include at least one number!";
}
if (!preg_match("/[a-zA-Z]+/", $pwd)) {
$errors[] = "Password must include at least one letter!";
}
function validateEmail($email) {
if(filter_var($email, FILTER_VALIDATE_EMAIL)) {
else {
validateEmail('peter.piper@iana.org');
validateEmail('first.last@example.123');
# php 7.x
<?php
$email_first = 'firstlast11@gmail.com';
$email_second ='firstlast@11gmail,com';
function validateEmail($email) {
$regex = "/^([a-zA-Z0-9\.]+@+[a-zA-Z]+(\.)+[a-zA-Z]{2,3})$/";
echo preg_match($regex, $email) ? "The email is valid"."<br>" :"The email is not valid";
validateEmail($email_first);
validateEmail($email_second);
?>