Home » PHP, Regular Expression2 March 2008

PHP: Validate Email, Name, Price, Age using Regular Expression & filter_var

This article contains PHP code with regular expression to validate different numbers and strings. Integer validation can be done for validating age. Decimal validation can be done for validating price. Simple String validation can be done for validating name. Email validation can be done for validating email.

preg_match PHP function is used to perform the regular expression match.

Validate Name

Validate Age

Validate Price

Allowing only 2 digits after dot. Price can be decimal or without decimal. 9 and 9.99 = both are true.

Validate Email

If you are using PHP 5.2 and above then you can use filter_var function to validate name, age and email. This has made validation a lot more easier task. We don’t need to write any regular expression code while using filter_var function.

Validate Email

In the above example, filter “FILTER_VALIDATE_EMAIL” is used to validate email. Similarly, there are different other filters to validate integer, boolean, URL, IP, etc.

FILTER_VALIDATE_BOOLEAN
FILTER_VALIDATE_FLOAT
FILTER_VALIDATE_INT
FILTER_VALIDATE_IP
FILTER_VALIDATE_URL

Hope it helps.
Thanks.

Regular Expression

Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed
  • jane

    cool, next time you need to paste formated code to anyone or a website try http://textsnip.com

  • There are two flaw in your validation which i’ve found…please improve them
    1) Name doesnot validate the names like D’souza.
    2) And in the email field if i put info@roshanbh , it will validate that email as well. For better email validation check this out..
    http://roshanbh.com.np/2008/02/email-validation-php.html

  • thanks again. well, i will leave to the user/reader of this code to use their mind/logic and make modification/improvement in my code.. ;-)

  • sudeep

    Ya , mukesh u have posted a good source about regular expression. I just wanted to point out that it will be difficult if u use the regular expression directly into the code,, so before using into the code u can use your regular expression into the software like regexcoach so that u can easily validate your regular expression.
    You can download from..
    http://www.snapfiles.com/download/dlregexcoach.html

  • Pingback: abcphp.com()

  • This is a simple example. It is easy to understand.
    And easy to work..

    Thanks a lot..!

  • custom php mysql programming

    good i got clear idea for my coding in validation…
    .
    THank yOU!!!