A-A+

javascript正则表达式验证各种文本框输入格式

2016年01月13日 前端设计 暂无评论 阅读 6 views 次

本文章介绍了类型各种文本输入框里面的输入格式进行正则表达式验证常用代码,主要是针对表单进行处理,学习正则表达式的朋友可以参考一下,不容错过此文章哦。

手机号为13开头的11位数字,代码如下:

  1. function f_MobilCheck(as_SourceString)  
  2. {  
  3.  if(as_SourceString.match(/^13[0-9]{9}$/g)) return true;  //手机号为13开头的11位数字  
  4.  else if(as_SourceString.match(/^[0]{1}[0-9]{2,3}[2-8]{1}[0-9]{5,7}$/g)) return true;  //小灵通为0开头的3-4位的区号+不以1和9开头的6-8位数字  
  5.  return false;  
  6. }  

请新增函数时加注释,尽量将同类的函数放在一起,便于查找。--Label_Name允许为空,在提示与输入框分离时使用。

  1. //---删除字符串左空格(包括全角空格)  
  2. function f_PubStrLTrim(as_SourceString)  
  3. {  
  4.  return as_SourceString.replace(/^[s ]*/,"");  
  5. }  
  6. //---删除字符串右空格(包括全角空格)-  
  7. function f_PubStrRTrim(as_SourceString)  
  8. {  
  9.  return as_SourceString.replace(/[s ]*$/,"");  
  10. }  
  11. //---删除字符串左右空格(包括全角空格)  
  12. function f_PubStrTrim(as_SourceString)  
  13. {  
  14.  return f_PubStrRTrim( f_PubStrLTrim(as_SourceString));    
  15. }  
  16. //---删除字符串的全部空格(包括全角空格)-  
  17. function f_PubStrTrimAll(as_SourceString)  
  18. {  
  19.  return as_SourceString.replace(/[s ]*/g,"");  
  20.    
  21. }  
  22. //-验证是否合法的电子邮箱地址合法:true---不合法:false-  
  23. function f_EmailCheck(as_SourceString)  
  24. {  
  25.  return as_SourceString.match(/^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$/g);  
  26. }  
  27. //--验证是否不含有非法的字符--不含有,即合法:true---不合法:false-  
  28. function f_StringCheck(as_SourceString)  
  29. {  
  30.  //非法字符--(双减号)/*(斜杠星号)'(单引号);(分号)"(双引号)%(百分号)<(左尖括号)>(右尖括号)  
  31.  if(as_SourceString.match(//*|-{2}|[';"%<>]+/)) return false;  
  32.  else return true;   
  33. }  
  34. //---验证字符串长度是否符合要求---0-为空,1-为小于下限,2-为大于上限,3-符合要求---  
  35. function f_StringLenCheck(as_SourceString, low_Length, up_Length)  
  36. {  
  37.  //字符串长度,单字节计1,双字节计和汉字等计2  
  38.  as_SourceString = as_SourceString.replace(/[^x00-xff]/g,"aa");  
  39.  if(as_SourceString.length == 0) return 0;  
  40.  else if(as_SourceString.length < low_Length) return 1;  
  41.  else if(as_SourceString.length > up_Length) return 2;  
  42.  else return 3;  
  43. }  
  44. //-验证是否全部是数字且不以0开头合法:true---不合法:false-  
  45. function f_NumericCheck(as_SourceString)  
  46. {  
  47.  return as_SourceString.match(/^[1-9]{1}d*$/g);  
  48. }  
  49. //-验证是否全部是数字可以0开头合法:true---不合法:false-  
  50. function f_NumericCheckAll(as_SourceString)  
  51. {  
  52.  return as_SourceString.match(/^[0-9]{1}d*$/g);  
  53. }  
  54. //-验证是否为标准的电话号码合法:true---不合法:false-  
  55. function f_MobilCheck(as_SourceString)  
  56. {  
  57.  if(as_SourceString.match(/^13[0-9]{9}$/g)) return true;  //手机号为13开头的11位数字  
  58.  else if(as_SourceString.match(/^[0]{1}[0-9]{2,3}[2-8]{1}[0-9]{5,7}$/g)) return true;  //小灵通为0开头的3-4位的区号+不以1和9开头的6-8位数字  
  59.  return false;  
  60. }  
  61. //-验证是否为标准的身份证号码合法:true---不合法:false-  
  62. function f_IDCardCheck(as_SourceString)  
  63. {  
  64.  return as_SourceString.match(/^[0-9]{6}[1-2]{1}[0-9]{3}[0-1]{1}[0-9]{1}[0-3]{1}[0-9]{1}[0-9]{3}[xX0-9]{1}$/g);  
  65. }  
  66. //--验证短日期格式  
  67. function f_DateShortCheck(as_SourceString)//2000-1-1或2000-01-01  
  68. {  
  69.  return as_SourceString.match(/^([1-2]{1})([0-9]{3})-(0?[1-9]|10|11|12)-(0?[1-9]|[1-2][0-9]|30|31)$/g);  
  70. }  

其它的参考一下

不能为空,代码如下:

  1. <input onblur="if(this.value.replace(/^ +| +$/g,'')=='')alert('不能为空!')">  

只能输入英文和数字

  1. <input onblur="if(/[^0-9a-zA-Z]/g.test(value))alert('有错')">  
  2. <input onkeyup="value=value.replace(/[^0-9a-zA-Z]/g,'')"/>  
  3. <input type="text" onkeyup="value=value.replace(/[^a-zA-Z0-9]/g,'')">  

判断字符由字母和数字,下划线,点号组成.且开头的只能是下划线和字母

/^([a-zA-z_]{1})([w]*)$/g.test(str)

只能输入数字

  1. <input name="text" type="text" id="NewPage" onKeyUp="value=value.replace(/D/g,'')" onafterpaste="value=value.replace(/D/g,'')" >  

只能输入中文

  1. <input type="text" onkeyup="value=value.replace(/[^u4E00-u9FA5]/g,'')">  

只能输入英文

  1. <input type="text" onkeyup="value=value.replace(/[^a-zA-Z]/g,'')">  
  2. <input type="text" onkeyup="value=value.replace(/[^a-zA-Z]/g,'')">  

只能输入中文、英文、数字、@符号和.符号

  1. <input type="text" onkeyup="value=value.replace(/[^a-zA-Z0-9u4E00-u9FA5@.]/g,'')">  

只允许输入英文,且不能粘贴也无法弹出粘贴菜单

  1. <input type="text" onkeyup="value=value.replace(/[^a-zA-Z]/g,'')" onkeydown="fncKeyStop(event)" onpaste="return false" oncontextmenu = "return false"/>  

中文:u4E00-u9FA5

数字:d、0-9

英文:a-z、A-Z

其它符号@,点或其它符号.也可以多个,用隔开就行了.

例如:

中、英文和数字加@符号加点符号:a-zA-Z0-9u4E00-u9FA5@.

若想在文本框里不能右键弹出菜单和不能粘贴进复制的信息的话就要在里输入 onKeyDown="fncKeyStop(event)" onpaste="return false" oncontextmenu="return false;"

总结:

我们不防仔细看看上面所有的正则表达式验证有大部份特别是表单input的验证都用了value.replace()来进行处理,像手机电话邮箱就用string.match()来处理,大家应该明白什么了吧

标签:

给我留言