自己写的一个客户端验证js
自己写的一个客户端验证js,有需要的朋友将就用,放在上面主要是怕换工作,找不到了又得重新写。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**
作者:胡进平
功能:日常验证类
时间:2008-07-19
*/
var Validator=new sg_validator();
function sg_validator()
{
}
/// <summary>
/// 必填字段验证
/// </summary>
/// <param name="control">要验证的控件</param>
/// <param name="message">弹出信息</param>
/// <returns>true|false</returns>
sg_validator.prototype.Required=function(control,message)
{
if(control==null)
return false;
if(control.value.trim()=='')
{
if(message !='' && message !=null)
alert(message);
else
alert('该项不能为空');
control.focus();
control.value='';
return false;
}
return true;
}
/// <summary>
/// 长度验证
/// </summary>
/// <param name="control">要验证的控件</param>
/// <param name="message">弹出信息</param>
/// <returns>true|false</returns>
sg_validator.prototype.Length=function(control,message,length)
{
if(control==null)
return false;
if(control.value.trim()=='')
{
if(message !='' && message !=null)
alert(message);
else
alert('该项不能为空');
control.focus();
control.value='';
return false;
}
if(strlen(control.value)>length)
{
alert('该项的字符长度限制在'+length+'个以内!');
control.focus();
control.value=curStr(control.value,length);
return false;
}
return true;
}
/// <summary>
/// 类型验证
/// </summary>
/// <param name="control">要验证的控件</param>
/// <param name="type">验证类型</param>
/// <param name="message">弹出信息</param>
/// <param name="minvalue">最小值</param>
/// <param name="maxvalue">最大值</param>
/// <returns>true|false</returns>
sg_validator.prototype.Type=function(control,type,message,minvalue,maxvalue)
{
if(control==null || type==null)
return false;
if(type.toLowerCase()=='number')
{
if(isNaN(control.value))
{
if(message !='' && message !=null)
alert(message);
else
alert('请输入数字');
control.focus();
control.value='';
return false;
}
else if(parseInt(control.value)<parseInt(minvalue) || parseInt(control.value)>parseInt(maxvalue))
{
if(message !='' && message !=null)
alert(message);
else
alert('输入的数字不在指定范围');
control.focus();
control.value='';
return false;
}
else
return true;
}
else if(type.toLowerCase()=='phone')
{
if(!this.Regex(control,/\d{3}-\d{8}|\d{4}-\d{7}/,'请输入指定格式的电话号码!'))
return false;
else
return true;
}
else if(type.toLowerCase()=='credit')
{
if(!this.Regex(control,/\d{15}|\d{18}/,'请输入正确的身份证号码!'))
return false;
else
return true;
}
else if(type.toLowerCase()=='qq')
{
if(!this.Regex(control,/[1-9][0-9]{4,}/,'请输入正确的QQ号码!'))
return false;
else
return true;
}
else if(type.toLowerCase()=='url')//url验证有点问题
{
if(!this.Regex(control,'/[a-zA-z]+://[^\s]*/','请输入正确的url地址!'))
return false;
else
return true;
}
else if(type.toLowerCase()=='date')
{
if(!this.Regex(control,/^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$/,'请输入正确的日期!'))
return false;
else
return true;
}
else if(type.toLowerCase()=='email')
{
if(!this.Regex(control,/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/,'请输入正确的Email地址!'))
return false;
else
return true;
}
else if(type.toLowerCase()=='zipcode')
{
if(!this.Regex(control,/[1-9]\d{5}(?!\d)/,'请输入正确的邮政编码!'))
return false;
else
return true;
}
else
return true;
}
/// <summary>
/// 类型验证
/// </summary>
/// <param name="control">要验证的控件</param>
/// <param name="regex">要匹配的正则表达式</param>
/// <param name="message">弹出信息</param>
/// <returns>true|false</returns>
sg_validator.prototype.Regex=function(control,regex,message)
{
if(control==null || regex==null)
return false;
if(!regex.test(control.value))
{
if(message !='' && message !=null)
alert(message);
else
alert('请输入指定格式');
control.focus();
control.value='';
return false;
}
return true;
}
/// <summary>
/// 计算(中英文)字符长度
/// </summary>
function strlen(str) {
var len = 0;
for (var i = 0; i < str.length; i++) {
if (str.charCodeAt(i) > 255) len += 2; else len ++;
}
return len;
}
/// <summary>
/// 截取指定长度的(中英文)字符
/// </summary>
function curStr(str,length)
{
var position=0;
var result=[];
for (var i = 0; i < str.length; i++)
{
if(position>=length)
break;
if (str.charCodeAt(i) > 255)
{
position += 2;
result.push(str.substr(i,1));
}
else
{
position++;
result.push(str.substr(i,1));
}
}
return result.join("");
}
客户端调用如下
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
function chkForm(oForm)
{
var txtJobName=$get('<%= txtJobName.ClientID%>');
var txtBeginDate=$get('<%=txtBeginDate.ClientID %>');
var txtEndDate=$get('<%= txtEndDate.ClientID%>');
var txtQuantity=$get('<%=txtQuantity.ClientID %>');
if(!Validator.Required(txtJobName,'请输入工作机会名称'))
return false;
if(!Validator.Length(txtJobName,null,100))
return false;
if(txtEndDate.value.trim()!='')
{
if(!Validator.Type(txtEndDate,'date'))
return false;
}
if(!Validator.Required(txtQuantity,'请输入工作机会数量'))
return false;
if(!Validator.Type(txtQuantity,'number','',1,10))
return false;
}
作者:Jackhuclan
出处:http://jackhuclan.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://jackhuclan.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。