asp.net(ajax)表单验证 函数包
/**********************************************************************************
*
* 功能说明:js表单验证函数库
* 作者: 刘功勋;
* 版本:v0.1(javascript);时间:2006-7-27
* 注意:文件为js文件,使用时,<script language="javascript" src="*.js"></script>
* *******************************************************************************/
/********************************************************************************
* 函数名称:resultremain
* 功能说明:根据传递值,改变表单提示状态
* 参 数:id:表单span id号;str:提示文字;cssclassname:改变提示颜色的css style class名称
*********************************************************************************/
function resultremain(id,stype,str,cssclassname)
{
switch (stype) {
//默认
case "normal" :
{
eval("span_"+id).classname=cssclassname;
break;
}
//正确
case "right" :
{
eval("span_"+id).innerhtml=str;
eval("span_"+id).classname=cssclassname;
break;
}
//错误
case "error":
{
eval("span_"+id).classname=cssclassname;
eval("error_"+id).innerhtml=str;
break;
}
//获得焦点
case "focus":
{
eval("span_"+id).innerhtml=str;
eval("span_"+id).classname=cssclassname;
}
default :
break;
}
}
/********************************************************************************
* 函数名称:onfocusform
* 功能说明:密码输入框获得焦点时显示提示状态
* 参 数:o:表单对象
* 调用方式:表单控件名称.attributes["onfocus"] = "return onfocuspwd(this);"; //(asp.net)
*********************************************************************************/
function onfocusform(o,str)
{
if(o){
resultremain(o.id,"focus",str,"formonfocus");
}
}
/********************************************************************************
* 函数名称:oncheckpwd
* 功能说明:当表单失去焦点时,触发相应的密码检测
* 参 数:o:表单对象
* 调用方式:表单名称.attributes["onfocus"] = "return onfocuspwd(this);"; //(asp.net)
*********************************************************************************/
function oncheckpwd(o){
var p = o.value;
if(p){
//测试密码长度,包括汉字密码
var len = 0;
var ary = escape(p).match(/(%u[0123456789abcdef]{4})+/g);
if(ary){
len = unescape(ary.join("")).length * 2;
p = unescape(escape(str).replace(/(%u[0123456789abcdef]{4})+/g,""));
len += p.length;
}else{
len = p.length;
}
if(len < 6 || len > 20){
resultremain(o.id,"error","<span style=\"color:#ff0000;\"><img src=\"/skin/main/images/icon_note.gif\" align=\"absmiddle\" /> 密码有误</span> ","formerr");
return false;
}else{
resultremain(o.id,"right","填写正确 ","formnormal");
return true;
}
}else{
resultremain(o.id,"error","<span style=\"color:#ff0000;\"><img src=\"/skin/main/images/icon_note.gif\" align=\"absmiddle\" /> 密码不能为空</span> ","formerr");
return false;
}
};
/**********************************************************************************
* 函数名称:strlen
* 功能说明:检测字符串长度
* 参 数:o:表单对象;len:字符串长度
* 调用方式:表单名称.attributes["onblur"] = "return strlen(this,30);"; //(asp.net)
***********************************************************************************/
function strlen(o,len)
{
var p = o.value;
if(p)
{
var l=p.length;
if(l>len)
{
resultremain(o.id,"error","<span style=\"color:#ff0000;\"><img src=\"/skin/main/images/icon_note.gif\" align=\"absmiddle\" /> 字符超过长度</span> ","formerr");
return false;
}
else
resultremain(o.id,"right","填写正确 ","formnormal");
return true;
}
else
{
resultremain(o.id,"error","<span style=\"color:#ff0000;\"><img src=\"/skin/main/images/icon_note.gif\" align=\"absmiddle\" /> 资料不能为空</span> ","formerr");
return false;
}
}
/**********************************************************************************
* 函数名称:oncheckpostcode(o)
* 功能说明:检测邮编是否合法
* 参 数:o:表单对象;
* 调用方式:表单名称.attributes["onblur"] = "return oncheckpostcode(this);"; //(asp.net)
***********************************************************************************/
function oncheckpostcode(o){
var str = o.value;
if(str){
var pattern = /^\d{6}$/;
if(!pattern.test(str))
{
resultremain(o.id,"error","<span style=\"color:#ff0000;\"><img src=\"/skin/main/images/icon_note.gif\" align=\"absmiddle\" /> 邮编输入不合法</span> ","formerr");
return false;
}else{
resultremain(o.id,"right","填写正确 ","formnormal");
return true;
}
}else{
resultremain(o.id,"error","<span style=\"color:#ff0000;\"><img src=\"/skin/main/images/icon_note.gif\" align=\"absmiddle\" /> 邮编不能为空</span> ","formerr");
return false;
}
}
/**********************************************************************************
* 函数名称:onchecktel(o)
* 功能说明:检测电话号码是否合法或传真
* 参 数:o:表单对象;
* 调用方式:表单名称.attributes["onblur"] = "return oncheckpostcode(this);"; //(asp.net)
***********************************************************************************/
function onchecktel(o){
var str = o.value;
if(str){
var pattern = /^((\d{3,4}-?\d{7,8})|(\d{7,8}))$/;
if(!pattern.test(str))
{
resultremain(o.id,"error","<span style=\"color:#ff0000;\"><img src=\"/skin/main/images/icon_note.gif\" align=\"absmiddle\" /> 号码格式不合法</span> ","formerr");
return false;
}else{
resultremain(o.id,"right","填写正确 ","formnormal");
return true;
}
}else{
resultremain(o.id,"error","<span style=\"color:#ff0000;\"><img src=\"/skin/main/images/icon_note.gif\" align=\"absmiddle\" /> 号码不能为空</span> ","formerr");
return false;
}
}
/**********************************************************************************
* 函数名称:oncheckmobile(o)
* 功能说明:检测手机号码是否合法
* 参 数:o:表单对象;
* 调用方式:表单名称.attributes["onblur"] = "return oncheckmobile(this);"; //(asp.net)
***********************************************************************************/
function oncheckmobile(o){
var str = o.value;
if(str){
var pattern = /^(0?13\d{9})$/;
if(!pattern.test(str))
{
resultremain(o.id,"error","<span style=\"color:#ff0000;\"><img src=\"/skin/main/images/icon_note.gif\" align=\"absmiddle\" /> 手机号码输入不合法</span> ","formerr");
return false;
}else{
resultremain(o.id,"right","填写正确 ","formnormal");
return true;
}
}else{
resultremain(o.id,"error","<span style=\"color:#ff0000;\"><img src=\"/skin/main/images/icon_note.gif\" align=\"absmiddle\" /> 手机号码不能为空</span> ","formerr");
return false;
}
}
/**********************************************************************************
* 函数名称:oncheckemail(o)
* 功能说明:检测e-mail是否合法
* 参 数:o:表单对象;
* 调用方式:表单名称.attributes["onblur"] = "return oncheckemail(this);"; //(asp.net)
***********************************************************************************/
function oncheckemail(o){
var em = o.value;
if(em){
var pattern = /^(\w|_|\.)+@((\w|_|\-)+\.)+(\w){2,3}$/;
if(!pattern.test(em))
{
resultremain(o.id,"error","<span style=\"color:#ff0000;\"><img src=\"/skin/main/images/icon_note.gif\" align=\"absmiddle\" /> e-mail输入不合法</span> ","formerr");
return false;
}
else{
resultremain(o.id,"right","填写正确 ","formnormal");
return true;
}
}else{
resultremain(o.id,"error","<span style=\"color:#ff0000;\"><img src=\"/skin/main/images/icon_note.gif\" align=\"absmiddle\" /> e-mail不能为空</span> ","formerr");
return false;
}
}
/**********************************************************************************
* 函数名称:ipcheck(o)
* 功能说明:检测ip地址是否合法
* 参 数:o:表单对象;
* 调用方式:表单名称.attributes["onblur"] = "return ipcheck(this);"; //(asp.net)
***********************************************************************************/
function ipcheck(o){
var ipstr=o.value;
if(ipstr){
var reg = /^((\d{1,3})(\.\d{1,3}){3})$/;
if(reg.test(ipstr))
{
var ary = ipstr.split(‘.‘);
for(key in ary)
{
if(parseint(ary[key]) > 255)
{
resultremain(o.id,"error","<span style=\"color:#ff0000;\"><img src=\"/skin/main/images/icon_note.gif\" align=\"absmiddle\" />ip输入不合法</span> ","formerr");
return false;
}
}
resultremain(o.id,"right","填写正确 ","formnormal");
return true;
}
else
{
resultremain(o.id,"error","<span style=\"color:#ff0000;\"><img src=\"/skin/main/images/icon_note.gif\" align=\"absmiddle\" />ip输入不合法</span> ","formerr");
return false;
}
}
else
{
resultremain(o.id,"error","<span style=\"color:#ff0000;\"><img src=\"/skin/main/images/icon_note.gif\" align=\"absmiddle\" /> ip不能为空</span> ","formerr");
return false;
}
}
/**********************************************************************************
* 函数名称:checknum(o)
* 功能说明:检测是否为纯数字
* 参 数:o:表单对象;
* 调用方式:表单名称.attributes["onblur"] = "return checknum(this);"; //(asp.net)
***********************************************************************************/
function checknum(o)
{
var str=o.value;
if(str)
{
var reg =/\d/;
if(!reg.test(str))
{
resultremain(o.id,"right","填写正确 ","formnormal");
return true;
}
else
{
resultremain(o.id,"error","<span style=\"color:#ff0000;\"><img src=\"/skin/main/images/icon_note.gif\" align=\"absmiddle\" /> 起始标识应为纯数字!</span> ","formerr");
return false;
}
}
else
{
resultremain(o.id,"error","<span style=\"color:#ff0000;\"><img src=\"/skin/main/images/icon_note.gif\" align=\"absmiddle\" /> 起始标识不能为空</span> ","formerr");
return false;
}
}
/**********************************************************************************
* 函数名称:checkchinese(o)
* 功能说明:检测是否为中文字符
* 参 数:o:表单对象;
* 调用方式:表单名称.attributes["onblur"] = "return checkchinese(this);"; //(asp.net)
***********************************************************************************/
function checkchinese(o)
{
var str=o.value;
if(str)
{
var reg =/[\u0391-\uffe5]/gi;
if(!reg.test(str))
{
resultremain(o.id,"right","填写正确 ","formnormal");
return true;
}
else
{
resultremain(o.id,"error","<span style=\"color:#ff0000;\"><img src=\"/skin/main/images/icon_note.gif\" align=\"absmiddle\" /> 含有中文字符!</span> ","formerr");
return false;
}
}
else
{
resultremain(o.id,"error","<span style=\"color:#ff0000;\"><img src=\"/skin/main/images/icon_note.gif\" align=\"absmiddle\" /> 不能为空</span> ","formerr");
return false;
}
}
/**********************************************************************************
* 函数名称: checkuid(o)
* 功能说明:检测用户名是否合法,暂没做ajax检测
* 参 数:o:表单对象;
* 调用方式:表单名称.attributes["onblur"] = "return checkuid(this);"; //(asp.net)
***********************************************************************************/
function checkuid(o){
var u = o.value;
if(u){
if(u.length < 4 || u.length > 20)
{
resultremain(o.id,"error","<span style=\"color:#ff0000;\"><img src=\"/skin/main/images/icon_note.gif\" align=\"absmiddle\" /> 用户名长度需在4-20之间!</span> ","formerr");
return false;
}
}else{
resultremain(o.id,"error","<span style=\"color:#ff0000;\"><img src=\"/skin/main/images/icon_note.gif\" align=\"absmiddle\" /> 用户名不能为空!</span> ","formerr");
return false;
}
var pattern = /^([a-za-z0-9]|[\u4e00-\ufa2d])+$/;
if(!pattern.test(u))
{
resultremain(o.id,"error","<span style=\"color:#ff0000;\"><img src=\"/skin/main/images/icon_note.gif\" align=\"absmiddle\" /> 用户名包含非法字符!</span> ","formerr");
return false;
}
else
{
resultremain(o.id,"right","填写正确 ","formnormal");
return true;
}
// if(u == o1.value && u != ""){
// regstyle(o.id,"right","此用户名可以使用");
// return false;
// }
// var flag = loadxml(hostname + "/callpage/query.aspx?op=queryuid&s=" + o.value);
// if(flag == ‘false‘){
// regstyle(o.id,"right","此用户名可以使用");
// o1.value = u;
// }else{
// regstyle(o.id,"error","这个用户名已被注册");
// return false;
// }
}
表单提交时,验证全部表单的例子:
function regform()
{
var falsestr="";
//检测密码
if(!oncheckpwd(form1.tb_u_password))
falsestr +="false|";
if(!oncheckpwd(form1.tb_u_password2))
falsestr +="false|";
if(form1.tb_u_password.value !=form1.tb_u_password2.value)
falsestr +="false|";
if(!oncheckemail(form1.tb_email))
falsestr +="false|";
if(!onchecktel(form1.tb_tel))
falsestr +="false|";
// if(!oncheckmobile(form1.tb_mobile))
// falsestr +="false|";
if(!checkuid(form1.tb_name))
falsestr +="false|";
//当falsestr中,包含false中,则返回false
if(falsestr.length>=5)
{
alert(‘表单中,有不符合规定的字符‘);
return false;
}
else
return true;