日期:2014-05-16  浏览次数:20297 次

常用的JS验证数字,电话,传真,邮箱,手机号码,邮编,日期
常用的js验证数字,电话号码,传真,邮箱,手机号码,邮编,日期
1、数字

function testisNum(object)
                       {
         
                        var s =document.getElementById(object.id).value;
             
                            if(s!="")
                            {
                                 if(isNaN(s))
                                {
                                 alert("请输入数字");
                                 object.value="";
                                 object.focus();
                                }
                            }
                        }



2、电话号码,传真

//校验普通电话、传真号码:可以“+”开头,除数字外,可含有“-”
            function isTel(object)
            {
            //国家代码(2到3位)-区号(2到3位)-电话号码(7到8位)-分机号(3位)"

             var s =document.getElementById(object.id).value;
             var pattern =/^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/;
             //var pattern =/(^[0-9]{3,4}\-[0-9]{7,8}$)|(^[0-9]{7,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/;
                 if(s!="")
                 {
                     if(!pattern.exec(s))
                     {
                      alert('请输入正确的电话号码:电话号码格式为国家代码(2到3位)-区号(2