Home » PHP, Regular Expression

Regular Expression check, Validation in PHP

2 March 2008 1,376 views Popularity: 3% Share/Bookmark

email

In this article, you will find php validation code for:

1) Interger Validation

2) String Validation

3) Decimal Validation (two digits after decimal)

4) Email Validation


<?php

if(isset($_POST['Submit']))
{
$name = $_POST['name'];
$age = $_POST['age'];
$email = $_POST['email'];
$amount = $_POST['amount'];

// we have used ereg() function for regular expression match
// ereg() function
// Searches a string for matches to the regular expression given in pattern in a case-sensitive way.
// for case insensitive search, we can use eregi() function

// ereg() function will retrun false if no matches were found

//checking name (string)
$check_name = ereg('^[a-zA-Z ]+$',$name);
if($check_name == false)
{
echo "<font color='red'>Invalid name: $name<br/></font>";
}

//checking amount (decimal value, allowing only 2 digits after dot)
$check_amount = ereg('^[0-9]+(\.[0-9]{2})$',$amount);
if($check_amount == false)
{
echo "<font color='red'>Invalid amount: $amount<br/></font>";
}

//checking age (integer)
$check_age = ereg('^[0-9]+$',$age);
if($check_age == false)
{
echo "<font color='red'>Invalid age: $age<br/></font>";
}

//checking email
$check_email = ereg('[a-zA-Z0-9_.]+@[a-zA-Z]+(\.[a-zA-Z]{3}(\.[a-zA-Z]{2})?)?',$email);
if($check_email == false)
{
echo "<font color='red'>Invalid email: $email<br/></font>";
}

//if everything is valid
if($check_name != false && $check_age != false && $check_amount != false && $check_email != false)
{
echo "<font color='green'>Everything is correct.";
echo "<br/>";
echo "Name: $name";
echo "<br/>";
echo "Age: $age";
echo "<br/>";
echo "Amount: $amount";
echo "<br/>";
echo "Email: $email";
echo "<br/>";
echo "</font>";
echo "<br/>";
}

}
?>

<html>
<head>
<title>Regular Expression Test</title>
</head>

<body>

<form method="post" action="regex.php">
<table>
<tr><td>Name: </td><td><input type="text" name="name" /></td></tr>
<tr><td>Age: </td><td><input type="text" name="age"  /></td></tr>
<tr><td>Amount: </td><td><input type="text" name="amount"  /> (eg. 11.22)</td></tr>
<tr><td>Email: </td><td><input type="text" name="email" /></td></tr>
<tr><td> </td><td><input type="submit" name="Submit" value="Submit" /></td></tr>
</table>
</form>

</body>
</html>

Related posts:

  1. Fun with Regular Expression
  2. W3C Validation: IFrame Error with XHTML 1.0 Strict Doctype
  3. Session Handling in PHP
  4. How to get(view) html source code of a website
  5. PHP: Read Write CSV
  6. How to change the source code and modify/parse a website?
  7. ASP.NET Error: A potentially dangerous Request.Form value was detected from the client
  8. Magento: How to check if current page is homepage?
  9. PHP: Easily send email with PHPMailer
  10. jQuery: Grey out background and preview image as popup
  • jane

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

  • http://roshanbh.com.np Roshan Bhattarai

    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

  • http://www.chapagain.com.np Mukesh

    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

  • http://abcphp.com/story/2426/ abcphp.com

    Regular Expression check, Validation in PHP | Mukesh Chapagain’s Blog…

    In this article, you will find php validation code for: 1) Interger Validation 2) String Validation 3) Decimal Validation (two digits after decimal) 4)…

  • http://www.phptechie.com/ contract php freelancer

    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!!!