函数调用方式
function check() {
var bb = document.getElementById("txt_id").value;
alert(ismobile(bb));
}
html代码
<input type="text" name="textfield" id="txt_id" />
<input type="submit" name="Submit" value="提交" onclick="check()" />
javascript脚本
function isalphanumber(str) {
var result = str.match(/^[a-zA-Z0-9]+$/);
if (result == null)
return false;
return true;
}
function isnumber(str)
{
return !isNaN(str);
}
function isint(str)
{
var result=str.match(/^(-|\+)?\d+$/);
if(result==null) return false;
return true;
}
function isdatetime(str)
{
var result=str.match(/^(\d{4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/);
if(result==null) return false;
var d= new Date(result[1], result[3]-1, result[4], result[5], result[6], result[7]);
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]);
}
function isdate(str){
var result=str.match(/^(\d{4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
if(result==null) return false;
var d=new Date(result[1], result[3]-1, result[4]);
return (d.getFullYear()==result[1] && d.getMonth()+1==result[3] && d.getDate()==result[4]);
}
function isemail(str)
{
var result=str.match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/);
if(result==null) return false;
return true;
}
function trim(str){
return str.replace(/(^\s*)|(\s*$)/g, "");
}
function strlen(str){
return str.replace(/[^\x00-\xff]/g, "**").length;
}
function ispostcode(str)
{
var result=str.match(/[1-9]\d{5}(?!\d)/);
if(result==null) return false;
return true;
}
function istell(str)
{
var result=str.match(/\d{3}-\d{8}|\d{4}-\d{7}/);
if(result==null) return false;
return true;
}
function isint1(str)
{
var result=str.match(/^[0-9]$|^([1-9])([0-9]){0,3}$|^10000$/);
if(result==null) return false;
return true;
}
function isqq(str)
{
var result=str.match(/[1-9][0-9]{4,}/);
if(result==null) return false;
return true;
}
function isidcard(str)
{
var result=str.match(/\d{15}|\d{18}/);
if(result==null) return false;
return true;
}
function checknull(field,sval)
{
if (field.value =="")
{
alert("请填写" + sval + "!");
field.focus();
return false;
}
return true;
}
function checkChar()
{
var keycode = event.keyCode;
if(!(keycode>=48&&keycode<=57))
{
return false;
}
}
/***************************************************************************************************************************
中国电话号码验证
匹配形式如: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代码不被下载
// 基本正则表达式///
//非空验证
function NotNull(str) {
return (str != "");
}
function checkEmail(str) {
isEmail1 = /^\w+([\.\-]\w+)*\@\w+([\.\-]\w+)*\.\w+$/;
isEmail2 = /^.*@[^_]*$/;
return (isEmail1.test(str) && isEmail2.test(str));
}
function checkIDCard(str) {
isIDCard1 = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/;
isIDCard2 = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{4}$/;
return (isIDCard1.test(str) || isIDCard2.test(str));
}
function checkIP(str) {
IP = '(25[0-5]|2[0-4]\\d|1\\d\\d|\\d\\d|\\d)';
IPdot = IP + '\\.';
isIPaddress = new RegExp('^' + IPdot + IPdot + IPdot + IP + '$');
return (isIPaddress.test(str));
}
function checkHomepage(str) {
isHomepage = /^\w+([\.\-]\w)*$/;
isHomepage = /^\w+(\.\w+)+\.\w+$/;
return (isHomepage.test(str));
}
function isNum(str) {
isNumber = /^([1-9]\d*(\.\d+)?)|(\d+(\.\d+))$/;
isNumber = /^\d+(\.\d+)?$/;
return (isNumber.test(str));
}
function isInt(str) {
isInteger = /^\d+$/;
return (isInteger.test(str));
}
?
1
2
3
4
5
function isChar(str) {
isCharacter = /^[A-Za-z]+$/;
return (isCharacter.test(str));
}
function checkBoolean(bv, i, w) {
if (bv == false) {
try {
i.focus();
} catch (e) {
}
alert(w);
return false;
}
return true
}
function checkElement_selected(item, alert_str) {
if (item.type == "select-one")
return checkElement_NotNull(item, alert_str);
if (alert_str.length == 0)
alert_str = item.title + "为必选项!";
rt = false;
if (item.length > 0) {
for (i = 0; i < item.length; i++) {
rt = rt || item[i].checked;
}
} else {
rt = item.checked
}
return checkBoolean(rt, item[0], alert_str);
return true;
}
function checkElement_NotNull(a, alert_str, g) {
v = a.value;
w = alert_str;
if (alert_str.length == 0)
w = a.title + "不能为空!";
return (checkValue_NotNull(v, a, w, g));
}
function checkValue_NotNull(v, i, w, g) {
if (g != "NOT_TRIM")
v = v.replace(/(^\s*)|(\s*$)/g, "");
bv = NotNull(v);
return (checkBoolean(bv, i, w));
}
function checkElement_IsEmail(a, alert_str, g) {
v = a.value;
w = alert_str;
if (alert_str.length == 0)
w = a.title + "不能为空!";
return (checkValue_IsEmail(v, a, w, g));
}
function checkValue_IsEmail(v, i, w, g) {
if (g != "NOT_TRIM")
v = v.replace(/(^\s*)|(\s*$)/g, "");
bv = checkEmail(v);
return (checkBoolean(bv, i, w));
}
function checkElement_IsIDCard(a, alert_str, g) {
v = a.value;
w = alert_str;
if (alert_str.length == 0)
w = a.title + "不能为空!";
return (checkValue_IsIDCard(v, a, w, g));
}
function checkValue_IsIDCard(v, i, w, g) {
if (g != "NOT_TRIM")
v = v.replace(/(^\s*)|(\s*$)/g, "");
bv = checkIDCard(v);
return (checkBoolean(bv, i, w));
}
function checkElement_IsIP(a, alert_str, g) {
v = a.value;
w = alert_str;
if (alert_str.length == 0)
w = a.title + "不能为空!";
return (checkValue_IsIP(v, a, w, g));
}
function checkValue_IsIP(v, i, w, g) {
if (g != "NOT_TRIM")
v = v.replace(/(^\s*)|(\s*$)/g, "");
bv = checkIP(v);
return (checkBoolean(bv, i, w));
}
function checkElement_IsNum(a, alert_str, g) {
v = a.value;
w = alert_str;
if (alert_str.length == 0)
w = a.title + "不能为空!";
return (checkValue_IsNum(v, a, w, g));
}
function checkValue_IsNum(v, i, w, g) {
if (g != "NOT_TRIM")
v = v.replace(/(^\s*)|(\s*$)/g, "");
bv = isNum(v);
return (checkBoolean(bv, i, w));
}
function checkElement_IsInt(a, alert_str, g) {
v = a.value;
w = alert_str;
if (alert_str.length == 0)
w = a.title + "不能为空!";
return (checkValue_IsInt(v, a, w, g));
}
function checkValue_IsInt(v, i, w, g) {
if (g != "NOT_TRIM")
v = v.replace(/(^\s*)|(\s*$)/g, "");
bv = isInt(v);
return (checkBoolean(bv, i, w));
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
function checkElement_IsChar(a, alert_str, g) {
v = a.value;
w = alert_str;
if (alert_str.length == 0)
w = a.title + "不能为空!";
return (checkValue_IsChar(v, a, w, g));
}
function checkValue_IsChar(v, i, w, g) {
if (g != "NOT_TRIM")
v = v.replace(/(^\s*)|(\s*$)/g, "");
bv = isChar(v);
return (checkBoolean(bv, i, w));
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
function checkElement_IsHomepage(a, alert_str, g) {
v = a.value;
w = alert_str;
if (alert_str.length == 0)
w = a.title + "不能为空!";
return (checkValue_IsHomepage(v, a, w, g));
}
function checkValue_IsHomepage(v, i, w, g) {
if (g != "NOT_TRIM")
v = v.replace(/(^\s*)|(\s*$)/g, "");
bv = checkHomepage(v);
return (checkBoolean(bv, i, w));
}