Simple validation functions using php and regex

In this post I provide you with php functions that validates username,password,first name,lastname and email.name_validate() is used to validate first name and last name,email_validate() to validate email,username_validate() to validate username and password_validate() to validate password.
Remember:

function returns 1 if the format is correct

function returns 0 if the entered format is incorrect.

Code is as follows:

<?php
function name_validate($str)
{
return preg_match("/^[A-Za-z]+$/",$str);
}
function email_validate($str)
{
return preg_match("/^[A-Za-z0-9_.+-]+@[A-Za-z0-9_.+-]+\.[a-zA-Z0-9-.]+$/",$str);
}
function username_validate($str)
{
return preg_match("/^[A-Za-z0-9_-]{6,12}+$/",$str);
}
function password_validate($str)
{
return preg_match("/^[A-Za-z0-9_@#$%\^&+=]{6,12}+$/",$str);
}
?>


To get a basic idea about regex read this.

Use these functions appropriately and if you have any doubts please ask through comments.


No comments:

Post a Comment