javascript验证表单输入



//原文地址:http://blog.csdn.net/linwei_1029/article/details/6903245

[javascript] view plain copy print ?
  1. function isMail(obj,str,allowNull) {  
  2.     var pattern = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;  
  3.     if(!isNotNull(obj,str,allowNull)) return false;  
  4.     if(!(pattern.test(obj.value))&&!isNull(obj)){  
  5.         //document.getElementById('doing').style.visibility='hidden';   
  6.         alert(str+" 不是合法电子邮件格式!");  
  7.         obj.focus();  
  8.         return false;  
  9.     }  
  10.     else return true;  
  11. }  
  12. //非法字符校验,以英文字母开头其后只能包含英文字母、数字及"_"  
  13. function isEN(obj,str,allowNull) {  
  14.     var pattern = /^([a-zA-Z])+([a-zA-Z0-9_]*)+$/;  
  15.     if(!isNotNull(obj,str,allowNull)) return false;  
  16.     if(!(pattern.test(obj.value))&&!isNull(obj)){  
  17.         //document.getElementById('doing').style.visibility='hidden';    
  18.         alert(str+" 必须以英文字母开头其后只能包含英文字母、数字及'_'");  
  19.         obj.focus();  
  20.         return false;  
  21.     }  
  22.     else return true;  
  23. }  
  24.   
  25. function isNotNull (obj,str,allowNull){  
  26.   if (isNull(obj) && !allowNull){  
  27.  // document.getElementById('doing').style.visibility='hidden';    
  28.     alert(str+" 不能为空!");  
  29.     obj.focus();  
  30.     return false;  
  31.   }  
  32.   else return true;  
  33. }  
  34.   
  35. function isNotNull1 (obj,str,allowNull){  
  36.   if ((isNull(obj)||trim(obj.value)=="null") && !allowNull){  
  37.     //document.getElementById('doing').style.visibility='hidden';    
  38.     alert(str+" 不能为空!");  
  39.     obj.focus();  
  40.     return false;  
  41.   }  
  42.   else return true;  
  43. }  
  44.   
  45. function isNull(obj){  
  46.     if(!obj.value || trim(obj.value)==""return true;  
  47.     else return false;  
  48. }  
  49. //  
  50.   
  51. function isNumber(obj,str,allowNull) {  
  52.     var pattern =/^[-,+]{0,1}[0-9]{0,}[.]{0,1}[0-9]{0,}$/;  
  53.     if(!isNotNull(obj,str,allowNull)) return false;  
  54.     if(!(pattern.test(obj.value))&&!isNull(obj)){  
  55.     //  document.getElementById('doing').style.visibility='hidden';    
  56.         alert(str+" 不是数字格式!");  
  57.         obj.focus();  
  58.         return false;  
  59.     }  
  60.     else return true;  
  61. }  
  62.   
  63. function isInteger(obj,str,allowNull) {  
  64.     var pattern = /^-*\d+$/;  
  65.     if(!isNotNull(obj,str,allowNull)) return false;  
  66.     if(!(pattern.test(obj.value))&&!isNull(obj)){  
  67.     //  document.getElementById('doing').style.visibility='hidden';    
  68.         alert(str+" 不是整数格式!");  
  69.         obj.focus();  
  70.         return false;  
  71.     }  
  72.     else return true;  
  73. }  
  74.   
  75. function isIntegerInfo(obj,str,allowNull) {  
  76.     var pattern = /^-*\d+$/;  
  77.     if(!isNotNull(obj,str,allowNull)) return false;  
  78.     if(!(pattern.test(obj.value))&&!isNull(obj)){  
  79.         //document.getElementById('doing').style.visibility='hidden';    
  80.         alert(str);  
  81.         obj.focus();  
  82.         return false;  
  83.     }  
  84.     else return true;  
  85. }  
  86.   
  87.   
  88. function isDate(obj,str,allowNull) {  
  89.     var pattern = /^[1-9]\d{3}[/|-]((0[1-9])|(1(0|1|2))|([1-9]))[/|-](([0-2][1-9])|([1-2][0-9])|(3(0|1))|([1-9]))$/;  
  90.     if(!isNotNull(obj,str,allowNull)) return false;  
  91.     if(!(pattern.test(obj.value))&&!isNull(obj)){  
  92.         //document.getElementById('doing').style.visibility='hidden';    
  93.         alert(str+" 不是日期格式!");  
  94.         obj.focus();  
  95.         return false;  
  96.     }  
  97.     else return true;  
  98. }  
  99.   
  100.   
  101. function verifyPassword(obj1, obj2) {  
  102.   if (obj1.value != obj2.value) {  
  103.      // document.getElementById('doing').style.visibility='hidden';    
  104.       alert("输入的密码不一致!");  
  105.     return false;  
  106.   }  
  107.   return true;  
  108. }  
  109.   
  110. function checkMobile(obj,str,allowNull){  
  111.       var pattern=/^((\(\d{2,3}\))|(\d{3}\-))?13\d{9}$/;  
  112.     if(!isNotNull(obj,str,allowNull)) return false;  
  113.     if(!(pattern.test(obj.value))&&!isNull(obj)){  
  114.     //  document.getElementById('doing').style.visibility='hidden';       
  115.         alert(str+" 格式不对!");  
  116.         obj.focus();  
  117.         return false;  
  118.     }  
  119.     else return true;  
  120. }  
  121. /**********验证身份证号码的有效性***********/  
  122. function isIdCard(obj,str,allowNull){  
  123.     var pattern = /^\d{15}(\d{2}[A-Za-z0-9])?$/;  
  124.     if(!isNotNull(obj,str,allowNull)) return false;  
  125.     if(!(pattern.test(obj.value))&&!isNull(obj)){  
  126.      //   document.getElementById('doing').style.visibility='hidden';     
  127.         alert(str+" 不是正确的身份证号码!");    
  128.         obj.focus();  
  129.         return false;  
  130.     }  
  131.     else return true;  
  132. }  
  133.   
  134. /**********验证手机号码的有效性***********/  
  135. function isMobile(obj,str,allowNull){  
  136.     var pattern = /^((\(\d{3}\))|(\d{3}\-))?13\d{9}$/;  
  137.     if(!isNotNull(obj,str,allowNull)) return false;  
  138.     if(!(pattern.test(obj.value))&&!isNull(obj)){  
  139.      //  document.getElementById('doing').style.visibility='hidden';     
  140.         alert(str+" 不是正确的手机号码!");  
  141.         obj.focus();  
  142.         return false;  
  143.     }  
  144.     else return true;  
  145. }  
  146.   
  147. /**********验证电话号码的有效性***********/  
  148. function isTel(obj,str,allowNull){  
  149.     var pattern = /^((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}$/;  
  150.     if(!isNotNull(obj,str,allowNull)) return false;  
  151.     if(!(pattern.test(obj.value))&&!isNull(obj)){  
  152.     //  document.getElementById('doing').style.visibility='hidden';       
  153.         alert(str+" 不是正确的电话号码!");  
  154.         obj.focus();  
  155.         return false;  
  156.     }  
  157.     else return true;  
  158. }  
  159.   
  160. /**********验证email的有效性***********/  
  161. function isEmail(obj,str,allowNull){  
  162.     var pattern = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;  
  163.     if(!isNotNull(obj,str,allowNull)) return false;  
  164.     if(!(pattern.test(obj.value))&&!isNull(obj)){  
  165.       // document.getElementById('doing').style.visibility='hidden';      
  166.         alert(str+" 不是正确的E_mail!");  
  167.         obj.focus();  
  168.         return false;  
  169.     }  
  170.     else return true;  
  171. }  
  172.   
  173. /**********验证IP地址的有效性***********/  
  174. function isIp(obj,str,allowNull){  
  175.     if(!isNotNull(obj,str,allowNull)) return false;  
  176.      var check=function(v){try{return (v<=255 && v>=0)}catch(x){return false}};  
  177.      var re=obj.value.split(".");  
  178.      //return (re.length==4)?(check(re[0]) && check(re[1]) && check(re[2]) && check(re[3])):false;  
  179.         if((re.length==4)?(check(re[0]) && check(re[1]) && check(re[2]) && check(re[3])):false &&!isNull(obj)){  
  180.     //  document.getElementById('doing').style.visibility='hidden';           
  181.         alert(str+" 不是正确的IP!");  
  182.         obj.focus();  
  183.         return false;  
  184.     }  
  185.     else return true;  
  186. }  
  187.   
  188. /**********验证只能为数字或字母***********/  
  189. function isNumOrE(obj,str,allowNull){  
  190.     var pattern = new RegExp("^[a-zA-Z0-9]+{1}quot;);  
  191.     if(!isNotNull(obj,str,allowNull)) return false;  
  192.     if(!(pattern.test(obj.value))&&!isNull(obj)){  
  193.     //  document.getElementById('doing').style.visibility='hidden';       
  194.         alert(str+"只能数字或者字母!");  
  195.         obj.focus();  
  196.         return false;  
  197.     }  
  198.     else return true;  
  199. }  
  200.   
  201. /**********验证邮政编码的有效性***********/  
  202. function isZip(obj,str,allowNull){  
  203.     var pattern =  /^[1-9]\d{5}$/;  
  204.     if(!isNotNull(obj,str,allowNull)) return false;  
  205.     if(!(pattern.test(obj.value))&&!isNull(obj)){  
  206.     //  document.getElementById('doing').style.visibility='hidden';       
  207.         alert(str+" 不是正确的邮政编码!");  
  208.         obj.focus();  
  209.         return false;  
  210.     }  
  211.     else return true;  
  212. }  
  213. /**********去除左右空格***********/  
  214. function trim(str){  
  215.     return str.replace(/(^\s*)|(\s*$)/g, "");  
  216. }  
  217. /* 
  218. function isNumber(obj) { 
  219.     var pattern =/^[-,+]{0,1}[0-9]{0,}[.]{0,1}[0-9]{0,}$/; 
  220.     var oldValue = obj.value; 
  221.     if(!(pattern.test(obj.value))){ 
  222.         document.getElementById('doing').style.visibility='hidden';   
  223.         alert("不是数字格式!"); 
  224.         obj.value = oldValue; 
  225.         obj.focus(); 
  226.         return false; 
  227.     } 
  228.     else return true; 
  229. }*/  
  230. function strToDate(str)  
  231. {  
  232.   var arys= new Array();  
  233.   arys=str.split('-');  
  234.   var newDate=new Date(arys[0],arys[1],arys[2]);   
  235.   return newDate;  
  236. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值