Regular Expression

Following are the important notations of regular expression:

[ ] matches only one character position with provided in between 
- it is used to match limit range of char or number provided in [ ] e.g [0-9]
^ it is used to negate the expression if it is inside of [ ] e.g [^fF]
^ outside [ ] means to match only at the begenning of target string
$ user to match at the end of target string
. used to match any char in this position
\ escape character
? matches preceding char occurance 0 or 1 times only
* matches preceding char occurance 0 or more times only
+ matches preceding char occurance 1 or more times
{ } matches preceding char occurance n times exactly e.g [char]{n}

HTML5 Form Validation With Regex

There are two ideas that enters the scene now:

  • The tag has new type attribute values like url, email, date, telephone number, and color.
  • The tag has the new attribute pattern where you can describe allowed input with a regex.

Here are three pragmatic (but not globally perfect) examples I created:

Strong password: <input title="at least eight symbols containing at least one number, one lower, and one upper letter" type="text" pattern="(?=.*d)(?=.*[a-z])(?=.*[A-Z]).{8,}" required />

Email address: <input type="text" title="email" required pattern="[^@]+@[^@]+.[a-zA-Z]{2,6}" />

Phone number: <input type="text" required pattern="(+?d[- .]*){7,13}" title="international, national or local phone number"/>

Easy way to data filter and validate in php

Before php 5.2.0 when we have to validate or filter user data, we normally use regex and some php functions. Some of those regex are difficult to understand. So normally most of the coders search in google to collect the correct regex to validate data and also use some php functions to filter data.

In php 5.2.0 a new extension is provided named filter to make these filter tasks much easy.  You can install it in your linux distro by simply typing in shell  pecl install filter

Before proceeding next at first check the available filters in your system:

echo '<pre>';
echo '</pre>';

Output in my system:

    [0] => int
    [1] => boolean
    [2] => float
    [3] => validate_regexp
    [4] => validate_url
    [5] => validate_email
    [6] => validate_ip
    [7] => string
    [8] => stripped
    [9] => encoded
    [10] => special_chars
    [11] => unsafe_raw
    [12] => email
    [13] => url
    [14] => number_int
    [15] => number_float
    [16] => magic_quotes
    [17] => callback

Continue reading