[code language=js]/**********************************************************************
* 常用JS验证函数
* QQ:12165743
* Email:wqb7156@163.com
* Date:2007-06-06
**********************************************************************
使用说明:正确匹配返回true,否则返回false
----------------------------------------------------------------------
函数列表:
CheckInput() 控件验证
IsLen() 判断长度
IsUsername() 验证用户名
IsPassword() 验证密码
IsEn() 验证是否为字母
IsCn() 验证是否为汉字
IsEmail() 验证E-mail
IsMSN() 验证MSN
IsQQ() 验证QQ号
IsHttpUrl() 验证网址
IsDoMainName() 验证域名
IsIpv4() 验证IPV4地址
IsImgURL() 验证图片地址
IsCellPhone() 验证手机号码
IsPhone() 验证固定电话
IsZipCode() 验证邮编
IsIdCard() 验证身份证号码
IsDateFormat() 验证日期格式YY-MM-DD
IsRangeTime() 验证时间格式HH:MM:SS
IsMoney() 验证金额格式
IsInt() 验证数字,并判断其范围
IsSearch() 验证关键字
-----------------------------------------------------------------------
使用例子:
<script language="javascript">
var email="username";
alert(email.IsEmail());
</script>
<input name="email" type="text" id="email" value="wqb7156#163.com" />
<div id="Megdiv"></div>
<script language="javascript">
if(!CheckInput("email","email","邮箱","notnull",'IsEmail',0,0,"Megdiv")) return flase;
</script>
----------------------------------------------------------------------*/
//控件验证 CheckInput(Id,Name,Title,Null,Format, LengthMin, LengthMax,Megdiv)
function CheckInput()
{
var Id=arguments[0];
var Name=arguments[1];
var Title=arguments[2];
var Null=arguments[3].toLowerCase();
var Format=arguments[4]
var LengthMin=arguments[5]
var LengthMax=arguments[6];
var Megdiv=(arguments[7]==null)?Id+'_meg':arguments[7];
var obj=document.getElementById(Id);
var Name=document.getElementsByName(Name);
var value=obj.value.replace(/(^\s*)|(\s*$)/g, "");
var type=obj.type.toLowerCase();
var showdiv=document.getElementById(Megdiv);
//是否为空,如不为空判断长度范围
if(Null=='notnull')
{
switch(type){
//文件域
case"file":
if( value=='' )
{
showdiv.innerHTML='×请选择你要上传的图片';
obj.focus();
return false;
}
//文本框
case"text":
if( value=='' )
{
showdiv.innerHTML='×'+Title+'不能为空';
obj.focus();
return false;
}
break;
//文本域
case"textarea":
if( value=='' )
{
showdiv.innerHTML='×'+Title+'不能为空';
obj.focus();
return false;
}
break;
//密码框
case"password":
if( value=='' )
{
showdiv.innerHTML='×'+Title+'不能为空';
obj.focus();
return false;
}
//确认密码
if(Id.substr(Id.length-2,2)=='re')
{
var pwd=Id.substr(0,Id.length-3);
if(document.getElementById(pwd).value!=value)
{
showdiv.innerHTML='×'+'两次输入的密码不相同';
obj.focus();
return false;
}
}
break;
//单选按钮
case"radio":
var flag=false;
for (var i=0;i<Name.length;i++)
{
if(Name.checked) flag=true;
//break;
}
if(!flag)
{
showdiv.innerHTML='×'+Title+'不能为空';
return false;
}
break;
//复选框
case"checkbox":
var flag=false;
for (var i=0;i<Name.length;i++)
{
if(Name.checked) flag=true;
//break;
}
if(!flag)
{
showdiv.innerHTML='×'+Title+'不能为空';
return false;
}
break;
//列表框
case"select-one":
if(obj.selectedIndex==0)
{
showdiv.innerHTML='×'+Title+'不能为空';
obj.focus();
return false;
}
break;
}
}else
{
//确认密码
if(type=='password'&&Id.substr(Id.length-2,2)=='re')
{
var pwd=Id.substr(0,Id.length-3);
if(document.getElementById(pwd).value!=value)
{
showdiv.innerHTML='×'+'两次输入的密码不相同';
obj.focus();
return false;
}
}
}
//长度判断
if(value!=''&&!value.IsLen(LengthMin,LengthMax))
{
showdiv.innerHTML='×'+Title+'长度必须在'+LengthMin+'和'+LengthMax+'之间';
obj.focus();
return false;
}
//验证数据格式
if(Format!='nocheck'&&value!='')
{
//选择函数
switch(Format)
{
case "IsLen": Format=value.IsLen(); break;
case "IsUsername": Format=value.IsUsername(); break;
case "IsPassword": Format=value.IsPassword(); break;
case "IsEn": Format=value.IsEn(); break;
case "IsCn": Format=value.IsCn(); break;
case "IsEmail": Format=value.IsEmail(); break;
case "IsMSN": Format=value.IsMSN(); break;
case "IsQQ": Format=value.IsQQ(); break;
case "IsHttpUrl": Format=value.IsHttpUrl(); break;
case "IsDoMainName": Format=value.IsDoMainName(); break;
case "IsIpv4": Format=value.IsIpv4(); break;
case "IsImgURL": Format=value.IsImgURL(); break;
case "IsCellPhone": Format=value.IsCellPhone(); break;
case "IsPhone": Format=value.IsPhone(); break;
case "IsZipCode": Format=value.IsZipCode(); break;
case "IsIdCard": Format=value.IsIdCard(); break;
case "IsDateFormat": Format=value.IsDateFormat(); break;
case "IsRangeTime": Format=value.IsRangeTime(); break;
case "IsMoney": Format=value.IsMoney(); break;
case "IsInt": Format=value.IsInt(); break;
case "IsSearch": Format=value.IsSearch(); break;
}
if(!Format)
{
showdiv.innerHTML='×'+Title+'格式不正确';
obj.focus();
return false;
}
}
showdiv.innerHTML="√";
return true;
}
//判断长度(长度不限为0)
String.prototype.IsLen = function()
{
var isRightFormat=false;
var minnum=arguments[0]?arguments[0]:0;
var maxnum=arguments[1]?arguments[1]:0;
isRightFormat = (minnum == 0 && maxnum ==0 ? true : (calculate_byte(this) >= minnum && calculate_byte(this) <= maxnum ? true : false));
return isRightFormat;
}
//验证用户名
String.prototype.IsUsername = function()
{
var myReg = /^[0-9a-zA-Z\-]+$/;
if(myReg.test(this)) return true;
return false;
}
//验证密码
String.prototype.IsPassword = function()
{
var myReg = /^[0-9a-zA-Z]+$/;
if(myReg.test(this)) return true;
return false;
}
//验证是否为字母
String.prototype.IsEn = function()
{
var myReg = /^[a-zA-Z]+$/;
if(myReg.test(this)) return true;
return false;
}
//验证是否为汉字
String.prototype.IsCn = function()
{
var myReg =/^[\u0391-\uFFE5]+$/;
if(myReg.test(this)) return true;
return false;
}
//验证E-mail
String.prototype.IsEmail = function()
{
var myReg = /([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)/;
if(myReg.test(this)) return true;
return false;
}
//验证MSN
String.prototype.IsMSN = function()
{
var myReg = /([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)/;
if(myReg.test(this)) return true;
return false;
}
//验证QQ号码
String.prototype.IsQQ = function()
{
var myReg =/^[1-9]\d{4,9}$/;
if(myReg.test(this)) return true;
return false;
}
//验证网址
String.prototype.IsHttpUrl = function()
{
var myReg =/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
if(myReg.test(this)) return true;
return false;
}
//验证域名
String.prototype.IsDoMainName = function()
{
var myReg =/^[0-9a-zA-Z]([0-9a-zA-Z\-]+\.){1,3}[a-zA-Z]{2,4}?$/;
if(myReg.test(this)) return true;
return false;
}
//验证IPV4地址
String.prototype.IsIpv4 = function()
{
var myReg =/^(2[0-5]{2}|1?[0-9]{1,2}).(2[0-5]{2}|1?[0-9]{1,2}).(2[0-5]{2}|1?[0-9]{1,2}).(2[0-5]{2}|1?[0-9]{1,2})$/;
if(myReg.test(this)) return true;
return false;
}
//验证图片地址(不支持由CGI动态生成的图片)
String.prototype.IsImgURL = function()
{
var myReg =/^\.(jpeg|jpg|gif|bmp|png|pcx|tiff|tga|lwf)$/;
if(myReg.test(this)) return true;
return false;
}
//验证手机号码
String.prototype.IsCellPhone = function()
{
var myReg =/^((\(\d{3}\))|(\d{3}\-))?1[3,5]\d{9}$/;
if(myReg.test(this)) return true;
return false;
}
//验证固定电话
String.prototype.IsPhone = function()
{
var myReg =/^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/;
if(myReg.test(this)) return true;
return false;
}
//验证邮编
String.prototype.IsZipCode = function()
{
var myReg =/[0-9]{6}/;
if(myReg.test(this)) return true;
return false;
}
//验证身份证号码
String.prototype.IsIdCard = function()
{
var myReg =/(^([\d]{15}|[\d]{18}|[\d]{17}[xX]{1})$)/;
if(myReg.test(this)) return true;
return false;
}
//验证日期格式YY-MM-DD
String.prototype.IsDateFormat = function()
{
var myReg =/^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$/;
if(myReg.test(this)) return true;
return false;
}
//验证时间格式HH:MM:SS
String.prototype.IsRangeTime = function()
{
var myReg =/^(\d{2}):(\d{2}):(\d{2})$/;
if(myReg.test(this)) return true;
return false;
}
//验证金额格式
String.prototype.IsMoney = function()
{
var myReg =/^[0-9]{1,8}[.]{0,1}[0-9]{0,2}$/;
if(myReg.test(this)) return true;
return false;
}
//字验证数字格式并判断数字的围(min:最小值;max:最大值.)
String.prototype.IsInt = function()
{
var isRightFormat=false;
var minnum=arguments[0]?arguments[0]:0;
var maxnum=arguments[1]?arguments[1]:0;
var myReg =/^[-\+]?\d+$/;
if(myReg.test(this))
{
isRightFormat = (minnum == 0 && maxnum ==0 ? true : (this > minnum && this < maxnum ? true : false));
}
return isRightFormat;
}
//验证搜索关键字
String.prototype.IsSearch = function()
{
var myReg=/^[\|\"\'<>,.*&@#$;:!^()]/;
if(myReg.test(this)) return false;
return true;
}
//js准确计算字符串长度
function calculate_byte( sTargetStr ) {
var sTmpStr, sTmpChar;
var nOriginLen = 0;
var nStrLength = 0;
sTmpStr = new String(sTargetStr);
nOriginLen = sTmpStr.length;
for ( var i=0 ; i < nOriginLen ; i++ ) {
sTmpChar = sTmpStr.charAt(i);
if (escape(sTmpChar).length > 4) {
nStrLength += 2;
} else if (sTmpChar!='\r') {
nStrLength ++;
}
}
return nStrLength;
}
[/code]
* 常用JS验证函数
* QQ:12165743
* Email:wqb7156@163.com
* Date:2007-06-06
**********************************************************************
使用说明:正确匹配返回true,否则返回false
----------------------------------------------------------------------
函数列表:
CheckInput() 控件验证
IsLen() 判断长度
IsUsername() 验证用户名
IsPassword() 验证密码
IsEn() 验证是否为字母
IsCn() 验证是否为汉字
IsEmail() 验证E-mail
IsMSN() 验证MSN
IsQQ() 验证QQ号
IsHttpUrl() 验证网址
IsDoMainName() 验证域名
IsIpv4() 验证IPV4地址
IsImgURL() 验证图片地址
IsCellPhone() 验证手机号码
IsPhone() 验证固定电话
IsZipCode() 验证邮编
IsIdCard() 验证身份证号码
IsDateFormat() 验证日期格式YY-MM-DD
IsRangeTime() 验证时间格式HH:MM:SS
IsMoney() 验证金额格式
IsInt() 验证数字,并判断其范围
IsSearch() 验证关键字
-----------------------------------------------------------------------
使用例子:
<script language="javascript">
var email="username";
alert(email.IsEmail());
</script>
<input name="email" type="text" id="email" value="wqb7156#163.com" />
<div id="Megdiv"></div>
<script language="javascript">
if(!CheckInput("email","email","邮箱","notnull",'IsEmail',0,0,"Megdiv")) return flase;
</script>
----------------------------------------------------------------------*/
//控件验证 CheckInput(Id,Name,Title,Null,Format, LengthMin, LengthMax,Megdiv)
function CheckInput()
{
var Id=arguments[0];
var Name=arguments[1];
var Title=arguments[2];
var Null=arguments[3].toLowerCase();
var Format=arguments[4]
var LengthMin=arguments[5]
var LengthMax=arguments[6];
var Megdiv=(arguments[7]==null)?Id+'_meg':arguments[7];
var obj=document.getElementById(Id);
var Name=document.getElementsByName(Name);
var value=obj.value.replace(/(^\s*)|(\s*$)/g, "");
var type=obj.type.toLowerCase();
var showdiv=document.getElementById(Megdiv);
//是否为空,如不为空判断长度范围
if(Null=='notnull')
{
switch(type){
//文件域
case"file":
if( value=='' )
{
showdiv.innerHTML='×请选择你要上传的图片';
obj.focus();
return false;
}
//文本框
case"text":
if( value=='' )
{
showdiv.innerHTML='×'+Title+'不能为空';
obj.focus();
return false;
}
break;
//文本域
case"textarea":
if( value=='' )
{
showdiv.innerHTML='×'+Title+'不能为空';
obj.focus();
return false;
}
break;
//密码框
case"password":
if( value=='' )
{
showdiv.innerHTML='×'+Title+'不能为空';
obj.focus();
return false;
}
//确认密码
if(Id.substr(Id.length-2,2)=='re')
{
var pwd=Id.substr(0,Id.length-3);
if(document.getElementById(pwd).value!=value)
{
showdiv.innerHTML='×'+'两次输入的密码不相同';
obj.focus();
return false;
}
}
break;
//单选按钮
case"radio":
var flag=false;
for (var i=0;i<Name.length;i++)
{
if(Name.checked) flag=true;
//break;
}
if(!flag)
{
showdiv.innerHTML='×'+Title+'不能为空';
return false;
}
break;
//复选框
case"checkbox":
var flag=false;
for (var i=0;i<Name.length;i++)
{
if(Name.checked) flag=true;
//break;
}
if(!flag)
{
showdiv.innerHTML='×'+Title+'不能为空';
return false;
}
break;
//列表框
case"select-one":
if(obj.selectedIndex==0)
{
showdiv.innerHTML='×'+Title+'不能为空';
obj.focus();
return false;
}
break;
}
}else
{
//确认密码
if(type=='password'&&Id.substr(Id.length-2,2)=='re')
{
var pwd=Id.substr(0,Id.length-3);
if(document.getElementById(pwd).value!=value)
{
showdiv.innerHTML='×'+'两次输入的密码不相同';
obj.focus();
return false;
}
}
}
//长度判断
if(value!=''&&!value.IsLen(LengthMin,LengthMax))
{
showdiv.innerHTML='×'+Title+'长度必须在'+LengthMin+'和'+LengthMax+'之间';
obj.focus();
return false;
}
//验证数据格式
if(Format!='nocheck'&&value!='')
{
//选择函数
switch(Format)
{
case "IsLen": Format=value.IsLen(); break;
case "IsUsername": Format=value.IsUsername(); break;
case "IsPassword": Format=value.IsPassword(); break;
case "IsEn": Format=value.IsEn(); break;
case "IsCn": Format=value.IsCn(); break;
case "IsEmail": Format=value.IsEmail(); break;
case "IsMSN": Format=value.IsMSN(); break;
case "IsQQ": Format=value.IsQQ(); break;
case "IsHttpUrl": Format=value.IsHttpUrl(); break;
case "IsDoMainName": Format=value.IsDoMainName(); break;
case "IsIpv4": Format=value.IsIpv4(); break;
case "IsImgURL": Format=value.IsImgURL(); break;
case "IsCellPhone": Format=value.IsCellPhone(); break;
case "IsPhone": Format=value.IsPhone(); break;
case "IsZipCode": Format=value.IsZipCode(); break;
case "IsIdCard": Format=value.IsIdCard(); break;
case "IsDateFormat": Format=value.IsDateFormat(); break;
case "IsRangeTime": Format=value.IsRangeTime(); break;
case "IsMoney": Format=value.IsMoney(); break;
case "IsInt": Format=value.IsInt(); break;
case "IsSearch": Format=value.IsSearch(); break;
}
if(!Format)
{
showdiv.innerHTML='×'+Title+'格式不正确';
obj.focus();
return false;
}
}
showdiv.innerHTML="√";
return true;
}
//判断长度(长度不限为0)
String.prototype.IsLen = function()
{
var isRightFormat=false;
var minnum=arguments[0]?arguments[0]:0;
var maxnum=arguments[1]?arguments[1]:0;
isRightFormat = (minnum == 0 && maxnum ==0 ? true : (calculate_byte(this) >= minnum && calculate_byte(this) <= maxnum ? true : false));
return isRightFormat;
}
//验证用户名
String.prototype.IsUsername = function()
{
var myReg = /^[0-9a-zA-Z\-]+$/;
if(myReg.test(this)) return true;
return false;
}
//验证密码
String.prototype.IsPassword = function()
{
var myReg = /^[0-9a-zA-Z]+$/;
if(myReg.test(this)) return true;
return false;
}
//验证是否为字母
String.prototype.IsEn = function()
{
var myReg = /^[a-zA-Z]+$/;
if(myReg.test(this)) return true;
return false;
}
//验证是否为汉字
String.prototype.IsCn = function()
{
var myReg =/^[\u0391-\uFFE5]+$/;
if(myReg.test(this)) return true;
return false;
}
//验证E-mail
String.prototype.IsEmail = function()
{
var myReg = /([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)/;
if(myReg.test(this)) return true;
return false;
}
//验证MSN
String.prototype.IsMSN = function()
{
var myReg = /([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)/;
if(myReg.test(this)) return true;
return false;
}
//验证QQ号码
String.prototype.IsQQ = function()
{
var myReg =/^[1-9]\d{4,9}$/;
if(myReg.test(this)) return true;
return false;
}
//验证网址
String.prototype.IsHttpUrl = function()
{
var myReg =/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
if(myReg.test(this)) return true;
return false;
}
//验证域名
String.prototype.IsDoMainName = function()
{
var myReg =/^[0-9a-zA-Z]([0-9a-zA-Z\-]+\.){1,3}[a-zA-Z]{2,4}?$/;
if(myReg.test(this)) return true;
return false;
}
//验证IPV4地址
String.prototype.IsIpv4 = function()
{
var myReg =/^(2[0-5]{2}|1?[0-9]{1,2}).(2[0-5]{2}|1?[0-9]{1,2}).(2[0-5]{2}|1?[0-9]{1,2}).(2[0-5]{2}|1?[0-9]{1,2})$/;
if(myReg.test(this)) return true;
return false;
}
//验证图片地址(不支持由CGI动态生成的图片)
String.prototype.IsImgURL = function()
{
var myReg =/^\.(jpeg|jpg|gif|bmp|png|pcx|tiff|tga|lwf)$/;
if(myReg.test(this)) return true;
return false;
}
//验证手机号码
String.prototype.IsCellPhone = function()
{
var myReg =/^((\(\d{3}\))|(\d{3}\-))?1[3,5]\d{9}$/;
if(myReg.test(this)) return true;
return false;
}
//验证固定电话
String.prototype.IsPhone = function()
{
var myReg =/^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/;
if(myReg.test(this)) return true;
return false;
}
//验证邮编
String.prototype.IsZipCode = function()
{
var myReg =/[0-9]{6}/;
if(myReg.test(this)) return true;
return false;
}
//验证身份证号码
String.prototype.IsIdCard = function()
{
var myReg =/(^([\d]{15}|[\d]{18}|[\d]{17}[xX]{1})$)/;
if(myReg.test(this)) return true;
return false;
}
//验证日期格式YY-MM-DD
String.prototype.IsDateFormat = function()
{
var myReg =/^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}$/;
if(myReg.test(this)) return true;
return false;
}
//验证时间格式HH:MM:SS
String.prototype.IsRangeTime = function()
{
var myReg =/^(\d{2}):(\d{2}):(\d{2})$/;
if(myReg.test(this)) return true;
return false;
}
//验证金额格式
String.prototype.IsMoney = function()
{
var myReg =/^[0-9]{1,8}[.]{0,1}[0-9]{0,2}$/;
if(myReg.test(this)) return true;
return false;
}
//字验证数字格式并判断数字的围(min:最小值;max:最大值.)
String.prototype.IsInt = function()
{
var isRightFormat=false;
var minnum=arguments[0]?arguments[0]:0;
var maxnum=arguments[1]?arguments[1]:0;
var myReg =/^[-\+]?\d+$/;
if(myReg.test(this))
{
isRightFormat = (minnum == 0 && maxnum ==0 ? true : (this > minnum && this < maxnum ? true : false));
}
return isRightFormat;
}
//验证搜索关键字
String.prototype.IsSearch = function()
{
var myReg=/^[\|\"\'<>,.*&@#$;:!^()]/;
if(myReg.test(this)) return false;
return true;
}
//js准确计算字符串长度
function calculate_byte( sTargetStr ) {
var sTmpStr, sTmpChar;
var nOriginLen = 0;
var nStrLength = 0;
sTmpStr = new String(sTargetStr);
nOriginLen = sTmpStr.length;
for ( var i=0 ; i < nOriginLen ; i++ ) {
sTmpChar = sTmpStr.charAt(i);
if (escape(sTmpChar).length > 4) {
nStrLength += 2;
} else if (sTmpChar!='\r') {
nStrLength ++;
}
}
return nStrLength;
}
[/code]