---
title: "JS正则表达式验证数字，整数，email"
author: "iammonster"
date: "2007-10-13T11:45:00Z"
tags: []
slug: "js-email"
draft: false
lastmod: "2007-10-13T11:45:00Z"
id: 273294
url: "https://jiajun.iteye.com/blog/273294"
---

说明：

元字符是正则表达式语法的一部分，有这些：

( [ { \\ ^ $ | ) ? * + .

任何时候使用他们都要对它们进行转义，比如： var regStr=/\\?/;

但是：如果不用上面的“字面量语法”的时候就要进行双重转义，如：var regStr="\\\\?";

在用JS中预定义的特殊字符时候，如：\\t，\\n；还有一组预定义字符类如：.，\\d，\\D，\\w；更应该注意转义。

我在复制代码的时候选的是HTML，\\\\就被过滤成\\了，所以会出现差异，这也是前面为什么网友们发现验证不对的地方。下面用文本粘贴：

正确的如下：

&lt;html&gt;  
&lt;head&gt;  
&lt;title&gt;正则表达式验证示例&lt;/title&gt;  
&lt;script language="JavaScript"&gt;  
&lt;!--  
function checkdata(){//检查函数  
  //检查是否数字  
    var txt = document.forms\[0].num.value;  
    if(txt.search("^-?\\\\d+([\\\\.\\\\d+)?$")!=0]()){  
        alert("请输入一个数字!");  
        document.forms\[0].num.select();  
        return false;  
    }  
    //检查是否整数  
    txt = document.forms\[0].int.value;  
    if(txt.search("^-?\\\\d+$")!=0){  
        alert("请输入一个整数!");  
        document.forms\[0].int.select();  
        return false;  
    }  
    //检查EMAIL是否合法  
    txt = document.forms\[0].email.value;  
    if(txt.search("^(?:\\\\w+\\\\.?)\*\\\\w+@(?:\\\\w+\\\\.?)\*\\\\w+$")!=0){  
        alert("请输入正确的电子邮件!");  
        document.forms\[0].email.select();  
        return false;  
    }  
    alert("检查通过!");  
    return true;  
}  
\--&gt;  
&lt;/script&gt;  
&lt;/head&gt;  
&lt;body&gt;  
&lt;p&gt;  
&lt;form action="" method="post" OnSubmit="return checkdata()"&gt;  
&lt;br&gt;请输入一个数字:&lt;input type="text" name="num"&gt;  
&lt;br&gt;请输入一个整数:&lt;input type="text" name="int"&gt;  
&lt;br&gt;请输入电子邮件:&lt;input type="text" name="email"&gt;  
&lt;br&gt;&lt;input type="submit" value="提交"&gt;  
&lt;form&gt;  
&lt;/body&gt;  
&lt;/html&gt;

再补充一些：常用验证：

&lt;script language="JavaScript"&gt;  
            &lt;!-- //对付老式浏览器所用注释  
             
   function testisNum(s){  
    var s =document.getElementById('num').value;  
    if(isNum(s))  
    {  
     alert("是数字");  
    }  
    else  
    {  
     alert("请出入数字");  
    }  
   }  
     
     
            ﻿//校验用户姓名：只能输入3-20个以字母和数字开头的字串  
            function isTrueName(s)  
            {  
             var pattern=/^\\w{3,20}$/;  
             if(pattern.exec(s))  
             {  
              return true;  
             }  
             return false;  
            }  
            //校验普通电话、传真号码：可以“+”开头，除数字外，可含有“-”  
            function isTel(s)  
            {  
             var pattern =/^\[+]{0,1}(\\d){1,3}\[ ]?(\[-]?((\\d)|\[ ]){1,12})+$/;  
             if(pattern.exec(s))  
             {  
              return true;  
             }  
             return false;  
            }  
            //校验手机号码：必须以数字开头，除数字外，可含有“-”  
            function isMobile(s)  
            {  
             //var patrn=/^\[+]{0,1}(\\d){1,3}\[ ]?(\[-]?((\\d)|\[ ]){1,12})+$/;  
             var patrn=/^((\\(\\d{0,3}\\))|(\\d{0,3}\\-))?13|15\\d{9}$/;  
             if (!patrn.exec(s)){  
               return false;  
               }  
              return true;  
            }  
            //校验(国内)邮政编码  
            function isPostalCode(s)  
            {  
             var patrn=/^\[0-9]{6}$/;  
             //var patrn=/^\[a-zA-Z0-9 ]{3,12}$/;  
              if (!patrn.exec(s)){  
               return false;  
               }  
              return true;  
            }  
             
            function isIP(s) //by zergling  
            {  
             var patrn=/^\[0-9.]{1,20}$/;  
              if (!patrn.exec(s)) {  
               return false;  
               }  
              return true;  
            }  
            //校验邮箱  
            function isEmail(s)  
            {  
             var patrn=/^\[a-zA-Z0-9\_\\-]{1,}@\[a-zA-Z0-9\_\\-]{1,}\\.\[a-zA-Z0-9\_\\-.]{1,}$/;  
              if (!patrn.exec(s)) {  
               return false;  
               }  
              return true;  
            }  
            //校验日期  
            function isdate(s)  
            {  
             var patrn=/^((\\d{2}((\[02468]\[048])|(\[13579]\[26]))\[\\-\\/\\s]?((((0?\[13578])|(1\[02]))\[\\-\\/\\s]?((0?\[1-9])|(\[1-2]\[0-9])|(3\[01])))|(((0?\[469])|(11))\[\\-\\/\\s]?((0?\[1-9])|(\[1-2]\[0-9])|(30)))|(0?2\[\\-\\/\\s]?((0?\[1-9])|(\[1-2]\[0-9])))))|(\\d{2}((\[02468]\[1235679])|(\[13579]\[01345789]))\[\\-\\/\\s]?((((0?\[13578])|(1\[02]))\[\\-\\/\\s]?((0?\[1-9])|(\[1-2]\[0-9])|(3\[01])))|(((0?\[469])|(11))\[\\-\\/\\s]?((0?\[1-9])|(\[1-2]\[0-9])|(30)))|(0?2\[\\-\\/\\s]?((0?\[1-9])|(1\[0-9])|(2\[0-8]))))))(\\s(((0?\[0-9])|(\[1-2]\[0-3]))\\:(\[0-5]?\[0-9])((\\s)|(\\:(\[0-5]?\[0-9])))))?$/;  
              if (!patrn.exec(s)){  
               return false;  
               }  
              return true;  
            }  
              
            //校验货币格式  
            function isCurrency(s)  
            {  
             var patrn=/^\\d+(\\.\\d+)?$/;  
              if (!patrn.exec(s)) {  
               return false;  
               }  
              return true;  
            }  
            //校验搜索关键字  
            function isSearch(s)  
            {  
             var patrn=/^\[^\`~!@#$%^&\*()+=|\\\\\\]\[\\]\\{\\}:;\\'\\,.&lt;&gt;?]{1}\[^\`~!@$%^&()+=|\\\\\\]\[\\]\\{\\}:;\\'\\,.&lt;&gt;?]{0,19}$/;  
              if (!patrn.exec(s)) {  
              return false;  
               }  
              return true;  
            }  
            //检查是否数字  
            function isNum(s)  
            {  
             var pattern = /^\\d+(\\.\\d+)?$/;  
             if(pattern.test(s))  
             {  
              return true;  
             }  
             return false;  
            }  
             //检查是否整数  
            function isInt(s)  
            {  
             var pattern = /^-?\\d+$/;  
             if(s.search(pattern)!=0)  
             {  
              return false;  
             }  
             return true;  
            }  
            --&gt;  
        &lt;/script&gt;

下面是以前错误的复制：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;html&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;head&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;title&gt;正则表达式验证示例&lt;/title&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)&lt;script language="JavaScript"&gt;...  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)&lt;!--  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)function checkdata() ...{//检查函数  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  //检查是否数字  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    var txt = document.forms\[0].num.value;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    if(txt.search("^\\d+(\\.\\d+)?$")!=0) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        alert("请输入一个数字!");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        document.forms\[0].num.select();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return false;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    //检查是否整数  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    txt = document.forms\[0].int.value;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    if(txt.search("^-?\\d+$")!=0) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        alert("请输入一个整数!");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        document.forms\[0].int.select();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return false;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    //检查EMAIL是否合法  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    txt = document.forms\[0].email.value;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    if(txt.search("^\[\\w-]+(\\.\[\\w-]+)\*@\[\\w-]+(\\.\[\\w-]+)+$")!=0) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        alert("请输入正确的电子邮件!");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        document.forms\[0].email.select();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        return false;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    alert("检查通过!");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    return true;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)--&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/script&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/head&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;body&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;p&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;form action="" method="post" OnSubmit="return checkdata()"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;br&gt;请输入一个数字:&lt;input type="text" name="num"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;br&gt;请输入一个整数:&lt;input type="text" name="int"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;br&gt;请输入电子邮件:&lt;input type="text" name="email"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;br&gt;&lt;input type="submit" value="提交"&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;form&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/body&gt;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)&lt;/html&gt;