A-A+
javascript正则表达式验证各种文本框输入格式
本文章介绍了类型各种文本输入框里面的输入格式进行正则表达式验证常用代码,主要是针对表单进行处理,学习正则表达式的朋友可以参考一下,不容错过此文章哦。
手机号为13开头的11位数字,代码如下:
- function f_MobilCheck(as_SourceString)
- {
- if(as_SourceString.match(/^13[0-9]{9}$/g)) return true; //手机号为13开头的11位数字
- 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位数字
- return false;
- }
请新增函数时加注释,尽量将同类的函数放在一起,便于查找。--Label_Name允许为空,在提示与输入框分离时使用。
- //---删除字符串左空格(包括全角空格)
- function f_PubStrLTrim(as_SourceString)
- {
- return as_SourceString.replace(/^[s ]*/,"");
- }
- //---删除字符串右空格(包括全角空格)-
- function f_PubStrRTrim(as_SourceString)
- {
- return as_SourceString.replace(/[s ]*$/,"");
- }
- //---删除字符串左右空格(包括全角空格)
- function f_PubStrTrim(as_SourceString)
- {
- return f_PubStrRTrim( f_PubStrLTrim(as_SourceString));
- }
- //---删除字符串的全部空格(包括全角空格)-
- function f_PubStrTrimAll(as_SourceString)
- {
- return as_SourceString.replace(/[s ]*/g,"");
- }
- //-验证是否合法的电子邮箱地址合法:true---不合法:false-
- function f_EmailCheck(as_SourceString)
- {
- 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);
- }
- //--验证是否不含有非法的字符--不含有,即合法:true---不合法:false-
- function f_StringCheck(as_SourceString)
- {
- //非法字符--(双减号)/*(斜杠星号)'(单引号);(分号)"(双引号)%(百分号)<(左尖括号)>(右尖括号)
- if(as_SourceString.match(//*|-{2}|[';"%<>]+/)) return false;
- else return true;
- }
- //---验证字符串长度是否符合要求---0-为空,1-为小于下限,2-为大于上限,3-符合要求---
- function f_StringLenCheck(as_SourceString, low_Length, up_Length)
- {
- //字符串长度,单字节计1,双字节计和汉字等计2
- as_SourceString = as_SourceString.replace(/[^x00-xff]/g,"aa");
- if(as_SourceString.length == 0) return 0;
- else if(as_SourceString.length < low_Length) return 1;
- else if(as_SourceString.length > up_Length) return 2;
- else return 3;
- }
- //-验证是否全部是数字且不以0开头合法:true---不合法:false-
- function f_NumericCheck(as_SourceString)
- {
- return as_SourceString.match(/^[1-9]{1}d*$/g);
- }
- //-验证是否全部是数字可以0开头合法:true---不合法:false-
- function f_NumericCheckAll(as_SourceString)
- {
- return as_SourceString.match(/^[0-9]{1}d*$/g);
- }
- //-验证是否为标准的电话号码合法:true---不合法:false-
- function f_MobilCheck(as_SourceString)
- {
- if(as_SourceString.match(/^13[0-9]{9}$/g)) return true; //手机号为13开头的11位数字
- 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位数字
- return false;
- }
- //-验证是否为标准的身份证号码合法:true---不合法:false-
- function f_IDCardCheck(as_SourceString)
- {
- 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);
- }
- //--验证短日期格式
- function f_DateShortCheck(as_SourceString)//2000-1-1或2000-01-01
- {
- 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);
- }
其它的参考一下
不能为空,代码如下:
- <input onblur="if(this.value.replace(/^ +| +$/g,'')=='')alert('不能为空!')">
只能输入英文和数字
- <input onblur="if(/[^0-9a-zA-Z]/g.test(value))alert('有错')">
- <input onkeyup="value=value.replace(/[^0-9a-zA-Z]/g,'')"/>
- <input type="text" onkeyup="value=value.replace(/[^a-zA-Z0-9]/g,'')">
判断字符由字母和数字,下划线,点号组成.且开头的只能是下划线和字母
/^([a-zA-z_]{1})([w]*)$/g.test(str)
只能输入数字
- <input name="text" type="text" id="NewPage" onKeyUp="value=value.replace(/D/g,'')" onafterpaste="value=value.replace(/D/g,'')" >
只能输入中文
- <input type="text" onkeyup="value=value.replace(/[^u4E00-u9FA5]/g,'')">
只能输入英文
- <input type="text" onkeyup="value=value.replace(/[^a-zA-Z]/g,'')">
- <input type="text" onkeyup="value=value.replace(/[^a-zA-Z]/g,'')">
只能输入中文、英文、数字、@符号和.符号
- <input type="text" onkeyup="value=value.replace(/[^a-zA-Z0-9u4E00-u9FA5@.]/g,'')">
只允许输入英文,且不能粘贴也无法弹出粘贴菜单
- <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()来处理,大家应该明白什么了吧