iammonster 发表于 2013-2-7 16:47:39

JS正则表达式验证数字,整数,email

 
说明:
元字符是正则表达式语法的一部分,有这些:
( [ { \ ^ $ | ) ? * + .
任何时候使用他们都要对它们进行转义,比如: var regStr=/\?/;
但是:如果不用上面的“字面量语法”的时候就要进行双重转义,如:var regStr="\\?";
在用JS中预定义的特殊字符时候,如:\t,\n;还有一组预定义字符类如:.,\d,\D,\w;更应该注意转义。
我在复制代码的时候选的是HTML,\\就被过滤成\了,所以会出现差异,这也是前面为什么网友们发现验证不对的地方。下面用文本粘贴:
正确的如下:
<html>
<head>
<title>正则表达式验证示例</title>
<script language="JavaScript">
<!--
function checkdata(){//检查函数
  //检查是否数字
    var txt = document.forms.num.value;
    if(txt.search("^-?\\d+(\\.\\d+)?$")!=0){
        alert("请输入一个数字!");
        document.forms.num.select();
        return false;
    }
    //检查是否整数
    txt = document.forms.int.value;
    if(txt.search("^-?\\d+$")!=0){
        alert("请输入一个整数!");
        document.forms.int.select();
        return false;
    }
    //检查EMAIL是否合法
    txt = document.forms.email.value;
    if(txt.search("^(?:\\w+\\.?)*\\w+@(?:\\w+\\.?)*\\w+$")!=0){
        alert("请输入正确的电子邮件!");
        document.forms.email.select();
        return false;
    }
    alert("检查通过!");
    return true;
}
-->
</script>
</head>
<body>
<p>
<form action="" method="post" >
<br>请输入一个数字:<input type="text" name="num">
<br>请输入一个整数:<input type="text" name="int">
<br>请输入电子邮件:<input type="text" name="email">
<br><input type="submit" value="提交">
<form>
</body>
</html>
再补充一些:常用验证:
<script language="JavaScript">
            <!-- //对付老式浏览器所用注释
           
   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=/^{6}$/;
             //var patrn=/^{3,12}$/;
              if (!patrn.exec(s)){
               return false;
               }
              return true;
            }
           
            function isIP(s) //by zergling
            {
             var patrn=/^{1,20}$/;
              if (!patrn.exec(s)) {
               return false;
               }
              return true;
            }
            //校验邮箱
            function isEmail(s)
            {
             var patrn=/^{1,}@{1,}\.{1,}$/;
              if (!patrn.exec(s)) {
               return false;
               }
              return true;
            }
            //校验日期
            function isdate(s)
            {
             var patrn=/^((\d{2}(()|())[\-\/\s]?((((0?)|(1))[\-\/\s]?((0?)|()|(3)))|(((0?)|(11))[\-\/\s]?((0?)|()|(30)))|(0?2[\-\/\s]?((0?)|()))))|(\d{2}(()|())[\-\/\s]?((((0?)|(1))[\-\/\s]?((0?)|()|(3)))|(((0?)|(11))[\-\/\s]?((0?)|()|(30)))|(0?2[\-\/\s]?((0?)|(1)|(2))))))(\s(((0?)|())\:(?)((\s)|(\:(?)))))?$/;
              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=/^[^`~!@#$%^&*()+=|\\\][\]\{\}:;\'\,.<>?]{1}[^`~!@$%^&()+=|\\\][\]\{\}:;\'\,.<>?]{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;
            }
            -->
        </script>
下面是以前错误的复制:
<div style="padding-right: 5.4pt; padding-left: 5.4pt; background: #e6e6e6; padding-bottom: 4px; width: 95%; padding-top: 4px;">http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif<html>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif<head>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif<title>正则表达式验证示例</title>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif<script language="JavaScript">...
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif<!--
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.giffunction 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.gifhttp://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.gifhttp://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.gifhttp://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-->
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif</script>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif</head>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif<body>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif<p>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif<form action="" method="post" OnSubmit="return checkdata()">
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif<br>请输入一个数字:<input type="text" name="num">
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif<br>请输入一个整数:<input type="text" name="int">
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif<br>请输入电子邮件:<input type="text" name="email">
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif<br><input type="submit" value="提交">
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif<form>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif</body>
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif</html>
页: [1]
查看完整版本: JS正则表达式验证数字,整数,email