Two regular expression verification methods for PHP user registered mailbox

Generally, when we register or log in to an account on the web, we need to verify not only whether the field is empty or long, but also whether it is in the format of the mailbox for some special fields, such as email, such as @ symbol. Just now, when practicing the PHP program script making member registration function, we used the mailbox account to verify the regular expression, which can filter the mailbox rules properly.

Here you can use two methods, one is to use preg_ One is to judge by JS.

First, PHP method

preg_ match(“/^[0-9a-zA-Z]+@(([0-9a-zA-Z]+)[.])+[a-z]{2,4}$/i”,$email );


function isEmail($email){
if(preg_ match(“/^[0-9a-zA-Z]+@(([0-9a-zA-Z]+)[.])+[a-z]{2,4}$/i”,$email )){
Return ‘is a mailbox’; < br > the}      else{
Return ‘is not a mailbox’; < br > the}

Or directly use if to judge:

if(!preg_ match(“/^[0-9a-zA-Z]+@(([0-9a-zA-Z]+)[.])+[a-z]{2,4}$/i”,$email )){
Error: email format error. <a href=”j avascript:history.back (- 1); “>return to <& A >‘”; < br > 1}

Second, JS judgment method

myreg = /^([a-zA-Z0-9]+[_ |_ |.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_ |_ |.]?)*[a-zA-Z0-9]+.[a-zA-Z]{2,4}$/;


<script type=”text/javascript”>

function isEmail(val){

var myreg = /^([a-zA-Z0-9]+[_ |_ |.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_ |_ |.]?)*[a-zA-Z0-9]+.[a-zA-Z]{2,4}$/;

if(! myreg.test (val))

Return ‘is not a mailbox’

Return ‘is a mailbox’


alert( isEmail(‘ ‘) );


According to our actual needs and project use, we used the first method.

Related Article
Two methods of using CSS style to intercept the length of the title do not need to use functions

For example, when we make the website template, or choose to download to a good default, maybe due to the Read more

Method to solve the disorder of Chinese content in MySQL database submitted by PHP

Whether it is ASP or PHP programming language, or even other web languages, the basic application is nothing more than Read more

Three common methods of PHP connecting MySQL database

For PHP entry-level users, we only need to master the basic database write, read, edit, delete and other basic operations. Read more

Solve the problem of Chinese garbled in VIM edit file under Linux system

If we need to edit the files in the Linux system, we may download them locally, open them with an Read more

Leave a Reply

Your email address will not be published. Required fields are marked *

To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image