/**
* 格式验证
* 目前支持格式有:
* 整数[number]、浮点数[floatNumber]
* Email[email]、IP地址[IP]
* 汉字[chinese]、URL[url]
*
* @param format 验证格式
* @param checkStr 待验证字符串
* @param msgPop 显示提示信息的控件名
*
*/
function checkFormat(format,checkStr,msgPop)
{
var regExp='';
//----正则表达式----------------------------------------------------
//匹配【整数】
var number=/^\d+$/;
//匹配【浮点数】
var floatNumber=/^((\d+)(\.\d{0,2})?)?$/;
//匹配【Email】
var email=/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
//匹配【IP地址】
var ip=/^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$/;
//匹配【汉字】
var chinese=/[\u4e00-\u9fa5]/;
//匹配【URL】
var url=/(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$/;
//判断验证格式
if(format=='number')
regExp = number;
else if(format=='floatNumber')
regExp = floatNumber;
else if(format=='email')
regExp = email;
else if(format=='ip')
regExp = ip;
else if(format=='chinese')
regExp = chinese;
else if(format=='url')
regExp = url;
//正则表达式验证
if(regExp.test(checkStr))
{
if(msgPop!=null)
{
document.getElementById(msgPop).innerHTML = 'ok!';
document.getElementById(msgPop).style.color="green";
}
else
alert('ok!');
return true;
}
else
{
if(msgPop!=null)
{
document.getElementById(msgPop).innerHTML = '囊咯的嘛,'+format+'格式错了!!';
document.getElementById(msgPop).style.color="red";
}
else
alert('囊咯的嘛,'+format+'格式错了!!');
return false;
}
}
//校验是否全由数字组成
function isDigit(s)
{
var patrn=/^[0-9]{1,20}$/;
if (!patrn.exec(s)) return false
return true
}
//校验登录名:只能输入5-20个以字母开头、可带数字、“_”、“.”的字串
function isRegisterUserName(s)
{
var patrn=/^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){4,19}$/;
if (!patrn.exec(s)) return false
return true
}
//校验用户姓名:只能输入1-30个以字母开头的字串
function isTrueName(s)
{
var patrn=/^[a-zA-Z]{1,30}$/;
if (!patrn.exec(s)) return false
return true
}
//校验密码:只能输入6-20个字母、数字、下划线
function isPasswd(s)
{
var patrn=/^(\w){6,20}$/;
if (!patrn.exec(s)) return false
return true
}
//校验普通电话、传真号码:可以“+”开头,除数字外,可含有“-”
function isTel(s)
{
//var patrn=/^[+]{0,1}(\d){1,3}[ ]?([-]?(\d){1,12})+$/;
var patrn=/^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/;
if (!patrn.exec(s)) return false
return true
}
//校验手机号码:必须以数字开头,除数字外,可含有“-”
function isMobil(s)
{
var patrn=/^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/;
if (!patrn.exec(s)) return false
return true
}
//校验邮政编码
function isPostalCode(s)
{
//var patrn=/^[a-zA-Z0-9]{3,12}$/;
var patrn=/^[a-zA-Z0-9 ]{3,12}$/;
if (!patrn.exec(s)) return false
return true
}
//校验搜索关键字
function isSearch(s)
{
var patrn=/^[^`~!@#$%^&*()+=|\\\][\]\{\}:;'\,.<>/?]{1}[^`~!@$%^&()+=|\\\][\]\{\}:;'\,.<>?]{0,19}$/;
if (!patrn.exec(s)) return false
return true
}
function isIP(s) //by zergling
{
var patrn=/^[0-9.]{1,20}$/;
if (!patrn.exec(s)) return false
return true
}