以下函数调用方式:
2 |
var bb
= document.getElementById( "txt_id" ).value; |
HTML代码:
<input type="text" name="textfield" id="txt_id" />
<input type="submit" name="Submit" value="提交" οnclick="check()" />
JavaScript代码:
003 |
function isalphanumber(str)
{ |
004 |
var result
= str.match(/^[a-zA-Z0-9]+$/); |
011 |
function isnumber(str) |
020 |
var result=str.match(/^(-|\+)?\d+$/); |
021 |
if (result== null ) return false ; |
027 |
function isdatetime(str) |
029 |
var result=str.match(/^(\d{4})(-|\/)(\d{1,2})\2(\d{1,2})
(\d{1,2}):(\d{1,2}):(\d{1,2})$/); |
030 |
if (result== null ) return false ; |
031 |
var d= new Date(result[1],
result[3]-1, result[4], result[5], result[6], result[7]); |
032 |
return (d.getFullYear()==result[1]&&(d.getMonth()+1)==result[3]&&d.getDate()==result[4]&&d.getHours()==result[5]&&d.getMinutes()==result[6]&&d.getSeconds()==result[7]); |
037 |
function isdate(str){ |
038 |
var result=str.match(/^(\d{4})(-|\/)(\d{1,2})\2(\d{1,2})$/); |
039 |
if (result== null ) return false ; |
040 |
var d= new Date(result[1],
result[3]-1, result[4]); |
041 |
return (d.getFullYear()==result[1]
&& d.getMonth()+1==result[3] && d.getDate()==result[4]); |
046 |
function isemail(str) |
048 |
var result=str.match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/); |
049 |
if (result== null ) return false ; |
056 |
return str.replace(/(^\s*)|(\s*$)/g, "" ); |
061 |
function strlen(str){ |
062 |
return str.replace(/[^\x00-\xff]/g, "**" ).length; |
067 |
function ispostcode(str) |
069 |
var result=str.match(/[1-9]\d{5}(?!\d)/); |
070 |
if (result== null ) return false ; |
076 |
var result=str.match(/\d{3}-\d{8}|\d{4}-\d{7}/); |
077 |
if (result== null ) return false ; |
084 |
var result=str.match(/^[0-9]$|^([1-9])([0-9]){0,3}$|^10000$/); |
085 |
if (result== null ) return false ; |
093 |
var result=str.match(/[1-9][0-9]{4,}/); |
094 |
if (result== null ) return false ; |
100 |
function isidcard(str) |
102 |
var result=str.match(/\d{15}|\d{18}/); |
103 |
if (result== null ) return false ; |
111 |
function checknull(field,sval) |
113 |
if (field.value
== "" ) |
115 |
alert( "请填写" +
sval + "!" ); |
130 |
var keycode
= event.keyCode; |
131 |
if (!(keycode>=48&&keycode<=57)) |
/***************************************************************************************************************************
中国电话号码验证
匹配形式如:0511-4405222 或者021-87888822 或者 021-44055520-555 或者 (0511)4405222
正则表达式 "((d{3,4})|d{3,4}-)?d{7,8}(-d{3})*"
中国邮政编码验证
匹配形式如:215421
正则表达式 "d{6}"
电子邮件验证
匹配形式如:justali@justdn.com
正则表达式 "w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*"
身份证验证
匹配形式如:15位或者18位身份证
正则表达式 "d{18}|d{15}"
常用数字验证
正则表达式
"d{n}" n为规定长度
"d{n,m}" n到m的长度范围
非法字符验证
匹配非法字符如:< > & / ' |
正则表达式 [^<>&/|'\]+
日期验证
匹配形式如:20030718,030718
范围:1900--2099
正则表达式((((19){1}|(20){1})d{2})|d{2})[01]{1}d{1}[0-3]{1}d{1}
匹配中文字符的正则表达式: [\u4e00-\u9fa5]
评注:匹配中文还真是个头疼的事,有了这个表达式就好办了
匹配双字节字符(包括汉字在内):[^\x00-\xff]
评注:可以用来计算字符串的长度(一个双字节字符长度计2,ASCII字符计1)
匹配空白行的正则表达式:\n\s*\r
评注:可以用来删除空白行
匹配HTML标记的正则表达式:< (\S*?)[^>]*>.*?|< .*? />
评注:网上流传的版本太糟糕,上面这个也仅仅能匹配部分,对于复杂的嵌套标记依旧无能为力
匹配首尾空白字符的正则表达式:^\s*|\s*$
评注:可以用来删除行首行尾的空白字符(包括空格、制表符、换页符等等),非常有用的表达式
匹配Email地址的正则表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
评注:表单验证时很实用
匹配网址URL的正则表达式:[a-zA-z]+://[^\s]*
评注:网上流传的版本功能很有限,上面这个基本可以满足需求
匹配帐号是否合法(字母开头,允许5-16字节,允许字母数字下划线):^[a-zA-Z][a-zA-Z0-9_]{4,15}$
评注:表单验证时很实用
匹配国内电话号码:\d{3}-\d{8}|\d{4}-\d{7}
评注:匹配形式如 0511-4405222 或 021-87888822
匹配腾讯QQ号:[1-9][0-9]{4,}
评注:腾讯QQ号从10000开始
匹配中国邮政编码:[1-9]\d{5}(?!\d)
评注:中国邮政编码为6位数字
匹配身份证:\d{15}|\d{18}
评注:中国的身份证为15位或18位
匹配ip地址:\d+\.\d+\.\d+\.\d+
评注:提取ip地址时有用
提取信息中的ip地址:
(\d+)\.(\d+)\.(\d+)\.(\d+)
提取信息中的中国手机号码:
(86)*0*13\d{9}
提取信息中的中国固定电话号码:
(\(\d{3,4}\)|\d{3,4}-|\s)?\d{8}
提取信息中的中国电话号码(包括移动和固定电话):
(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}
提取信息中的中国邮政编码:
[1-9]{1}(\d+){5}
提取信息中的中国身份证号码:
\d{18}|\d{15}
提取信息中的整数:
\d+
提取信息中的浮点数(即小数):
(-?\d*)\.?\d+
提取信息中的任何数字 :
(-?\d*)(\.\d+)?
提取信息中的中文字符串:
[\u4e00-\u9fa5]*
提取信息中的双字节字符串 (汉字):
[^\x00-\xff]*
提取信息中的英文字符串:
\w*
提取信息中的网络链接:
(h|H)(r|R)(e|E)(f|F) *= *('|")?(\w|\\|\/|\.)+('|"| *|>)?
提取信息中的邮件地址:
\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
提取信息中的图片链接:
(s|S)(r|R)(c|C) *= *('|")?(\w|\\|\/|\.)+('|"| *|>)?
匹配特定数字:
^[1-9]\d*$ //匹配正整数
^-[1-9]\d*$ //匹配负整数
^-?[1-9]\d*$ //匹配整数
^[1-9]\d*|0$ //匹配非负整数(正整数 + 0)
^-[1-9]\d*|0$ //匹配非正整数(负整数 + 0)
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$ //匹配正浮点数
^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$ //匹配负浮点数
^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$ //匹配浮点数
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$ //匹配非负浮点数(正浮点数 + 0)
^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$ //匹配非正浮点数(负浮点数 + 0)
评注:处理大量数据时有用,具体应用时注意修正
匹配特定字符串:
^[A-Za-z]+$ //匹配由26个英文字母组成的字符串
^[A-Z]+$ //匹配由26个英文字母的大写组成的字符串
^[a-z]+$ //匹配由26个英文字母的小写组成的字符串
^[A-Za-z0-9]+$ //匹配由数字和26个英文字母组成的字符串
^\w+$ //匹配由数字、26个英文字母或者下划线组成的字符串
评注:最基本也是最常用的一些表达式
////////////////////前4行程序用于保护js代码不被下载
// ////////////////////基本正则表达式///////////////////
//非空验证
1 |
function NotNull(str)
{ |
//邮件地址验证
1 |
function checkEmail(str)
{ |
3 |
isEmail1
= /^\w+([\.\-]\w+)*\@\w+([\.\-]\w+)*\.\w+$/; |
5 |
isEmail2
= /^.*@[^_]*$/; |
7 |
return (isEmail1.test(str)
&& isEmail2.test(str)); |
//身份证验证
01 |
function checkIDCard(str)
{ |
03 |
isIDCard1
= /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/; |
06 |
isIDCard2
= /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{4}$/; |
09 |
return (isIDCard1.test(str)
|| isIDCard2.test(str)); |
//IP验证
1 |
function checkIP(str)
{ |
2 |
IP
= '(25[0-5]|2[0-4]\\d|1\\d\\d|\\d\\d|\\d)' ; |
4 |
isIPaddress
= new RegExp( '^' +
IPdot + IPdot + IPdot + IP + '$' ); |
6 |
return (isIPaddress.test(str)); |
//主页(网址)验证
1 |
function checkHomepage(str)
{ |
3 |
isHomepage
= /^\w+([\.\-]\w)*$/; |
4 |
isHomepage
= /^\w+(\.\w+)+\.\w+$/; |
6 |
return (isHomepage.test(str)); |
//是否数字
2 |
isNumber
= /^([1-9]\d*(\.\d+)?)|(\d+(\.\d+))$/; |
3 |
isNumber
= /^\d+(\.\d+)?$/; |
5 |
return (isNumber.test(str)); |
//是否整数
4 |
return (isInteger.test(str)); |
//是否字母
2 |
isCharacter
= /^[A-Za-z]+$/; |
4 |
return (isCharacter.test(str)); |
/////////////////////基本弹出窗口///////////////////
01 |
function checkBoolean(bv,
i, w) { |
////////////////////元素和取值判断//////////////////// //
已选择
01 |
function checkElement_selected(item,
alert_str) { |
02 |
if (item.type
== "select-one" ) |
03 |
return checkElement_NotNull(item,
alert_str); |
04 |
if (alert_str.length
== 0) |
05 |
alert_str
= item.title + "为必选项!" ; |
07 |
if (item.length
> 0) { |
08 |
for (i
= 0; i < item.length; i++) { |
09 |
rt
= rt || item[i].checked; |
14 |
return checkBoolean(rt,
item[0], alert_str); |
// 不为空
01 |
function checkElement_NotNull(a,
alert_str, g) { |
04 |
if (alert_str.length
== 0) |
05 |
w
= a.title + "不能为空!" ; |
06 |
return (checkValue_NotNull(v,
a, w, g)); |
08 |
function checkValue_NotNull(v,
i, w, g) { |
10 |
v
= v.replace(/(^\s*)|(\s*$)/g, "" ); |
12 |
return (checkBoolean(bv,
i, w)); |
// 合法邮箱
01 |
function checkElement_IsEmail(a,
alert_str, g) { |
04 |
if (alert_str.length
== 0) |
05 |
w
= a.title + "不能为空!" ; |
06 |
return (checkValue_IsEmail(v,
a, w, g)); |
08 |
function checkValue_IsEmail(v,
i, w, g) { |
10 |
v
= v.replace(/(^\s*)|(\s*$)/g, "" ); |
12 |
return (checkBoolean(bv,
i, w)); |
// 合法身份证
01 |
function checkElement_IsIDCard(a,
alert_str, g) { |
04 |
if (alert_str.length
== 0) |
05 |
w
= a.title + "不能为空!" ; |
06 |
return (checkValue_IsIDCard(v,
a, w, g)); |
08 |
function checkValue_IsIDCard(v,
i, w, g) { |
10 |
v
= v.replace(/(^\s*)|(\s*$)/g, "" ); |
12 |
return (checkBoolean(bv,
i, w)); |
// 合法IP
01 |
function checkElement_IsIP(a,
alert_str, g) { |
04 |
if (alert_str.length
== 0) |
05 |
w
= a.title + "不能为空!" ; |
06 |
return (checkValue_IsIP(v,
a, w, g)); |
08 |
function checkValue_IsIP(v,
i, w, g) { |
10 |
v
= v.replace(/(^\s*)|(\s*$)/g, "" ); |
12 |
return (checkBoolean(bv,
i, w)); |
// 验证数字
02 |
function checkElement_IsNum(a,
alert_str, g) { |
05 |
if (alert_str.length
== 0) |
06 |
w
= a.title + "不能为空!" ; |
07 |
return (checkValue_IsNum(v,
a, w, g)); |
09 |
function checkValue_IsNum(v,
i, w, g) { |
11 |
v
= v.replace(/(^\s*)|(\s*$)/g, "" ); |
13 |
return (checkBoolean(bv,
i, w)); |
// 验证整数
02 |
function checkElement_IsInt(a,
alert_str, g) { |
05 |
if (alert_str.length
== 0) |
06 |
w
= a.title + "不能为空!" ; |
07 |
return (checkValue_IsInt(v,
a, w, g)); |
09 |
function checkValue_IsInt(v,
i, w, g) { |
11 |
v
= v.replace(/(^\s*)|(\s*$)/g, "" ); |
13 |
return (checkBoolean(bv,
i, w)); |
// 验证字母
01 |
function checkElement_IsChar(a,
alert_str, g) { |
04 |
if (alert_str.length
== 0) |
05 |
w
= a.title + "不能为空!" ; |
06 |
return (checkValue_IsChar(v,
a, w, g)); |
08 |
function checkValue_IsChar(v,
i, w, g) { |
10 |
v
= v.replace(/(^\s*)|(\s*$)/g, "" ); |
12 |
return (checkBoolean(bv,
i, w)); |
//合法主页
01 |
function checkElement_IsHomepage(a,
alert_str, g) { |
04 |
if (alert_str.length
== 0) |
05 |
w
= a.title + "不能为空!" ; |
06 |
return (checkValue_IsHomepage(v,
a, w, g)); |
08 |
function checkValue_IsHomepage(v,
i, w, g) { |
10 |
v
= v.replace(/(^\s*)|(\s*$)/g, "" ); |
11 |
bv
= checkHomepage(v); |
12 |
return (checkBoolean(bv,
i, w)); |