本文实例讲述了jquery实现用户信息修改验证输入方法。分享给大家供大家参考。具体如下:
var realnameflag = 0;var addressflag = 0;var zipflag=0;var cellphoneflag=0;var homephoneflag=0;var oldpasswordflag=1;var newpasswordflag=1;//判断emailfunction check_email(){ $(#showspan).hide(); var email = $.trim($(#email).val()); if (email == null || email == ) { emailflag = 1; $(#emailmsg).html(请输入邮箱信息); return; } // 判断输入框内是否为邮箱格式 if (email.replace(/[^\x00-\xff]/g, **).length = 50) { $(#emailmsg).html(邮箱长度不正确); emailflag = 1; return ; } var reg = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/; if (reg.test(email)) { $(#emailmsg).html(); emailflag = 0; return ; } else { $(#emailmsg).html(邮箱格式不正确); emailflag = 1; return ; }}function check_realname(){ $(#showspan).hide(); var realname = $.trim($(#realname).val()); // 判断是否为空 if (realname == null || realname == ) { $(#realnamemsg).html(请输入真实姓名,20个英文或10个汉字); realnameflag = 1; } else if (realname.indexof(··) != -1) { $(#realnamemsg).html(请输入真实姓名,20个英文或10个汉字); realnameflag = 1; // 姓名前后不能加· } else if (realname.substring(0, 1) == · || realname.substring(realname.length - 1) == ·) { realnameflag = 1; $(#realnamemsg).html(请输入真实姓名,20个英文或10个汉字); } else { var reg = new regexp(^([a-za-z]|[\\u4e00-\\u9fff])+$, g); if (!reg.test(realname)) { $(#realnamemsg).html(请输入真实姓名,20个英文或10个汉字); realnameflag = 1; } else if (realname.replace(/[^\x00-\xff]/g, **).length >= 4 && realname.replace(/[^\x00-\xff]/g, **).length 120) { addressflag = 1; $(#addressmsg).html(长度超长); } else { addressflag = 0; $(#addressmsg).html(); }}function check_zip(){ $(#showspan).hide(); var zip=$.trim($(#zip).val()); var re=/^[0-9]+$/; if(zip.length != 0 && (!re.test(zip) || zip.length != 6)) { zipflag=1; $(#zipmsg).html(请输入邮政编码,由6位数字组成); }else { zipflag=0; $(#zipmsg).html(); }}function check_oldpassword(){ $(#msgspan).html(); $(#newpsmsg).html(); $(#repsmsg).html(); var oldps = $.trim($(#oldpassword).val()); if(oldps == null || oldps == ) { oldpasswordflag = 1; $(#oldpsmsg).html(请输入旧密码); $(this).focus(); return false; } var re=/^.{6,16}$/; if(!re.test(oldps)) { oldpasswordflag = 1; $(#oldpsmsg).html(旧密码输入长度不正确); $(this).focus(); return false; } else { oldpasswordflag = 0; $(#oldpsmsg).html(); } }function check_newpassword(){ $(#oldpsmsg).html(); $(#repsmsg).html(); $(#msgspan).html(); $(#repeatpassword).val(); var newps = $.trim($(#newpassword).val()); if(newps == null || newps == ) { newpasswordflag=1; $(#newpsmsg).html(请输入新密码,由6-16位字符组成!); $(this).focus(); return false; } //var re=/^[a-za-z0-9_-]{6,16}$/; var re=/^.{6,16}$/; if (newps.replace(/[^\x00-\xff]/g, **).length 16) { newpasswordflag = 1; $(#newpsmsg).html(新密码长度不正确); $(this).focus(); return false; } if(!re.test(newps)) { newpasswordflag = 1; $(#newpsmsg).html(新密码长度不正确); $(this).focus(); return false; }else { newpasswordflag = 0; $(#newpsmsg).html(); } }function check_repeatpassword(){ $(#oldpsmsg).html(); $(#newpsmsg).html(); $(#msgspan).html(); if( $(#repeatpassword).val()==null || $.trim($(#repeatpassword).val()) ==) { $(#repsmsg).html(请输入重复新密码); $(this).focus(); newpasswordflag = 1; return false; }else if ( $.trim($(#repeatpassword).val()) != $.trim($(#newpassword).val())) { newpasswordflag = 1; $(#repsmsg).html(两次密码输入的不一致!); $(this).focus(); return false; }else { newpasswordflag = 0; $(#repsmsg).html(); }}function check_cardnum(){ $(#vcodemsg).html(); $(#psmsg).html(); $(#strmsg).hide(); var cardnum = $.trim( $(#cardnum).val()); if(cardnum == null || cardnum == ) { $(#nummsg).html(请输入卡号); cardnumflag = 1; return false; }else { $(#nummsg).html(); cardnumflag=0; }}function check_password(){ $(#vcodemsg).html(); $(#nummsg).html(); $(#strmsg).hide(); var password = $.trim( $(#password).val()); if(password == null || password == ) { passwordflag = 1; $(#psmsg).html(请输入密码); return false; }else { passwordflag = 0; $(#psmsg).html(); }}function check_vcode(){ $(#vcodemsg).html(); $(#psmsg).html(); $(#nummsg).html(); $(#strmsg).hide(); var vcode = $.trim( $(#vcode).val()); if(vcode == null || vcode ==) { vcodeflag = 1; $(#vcodemsg).html(请输入验证码); return false; } var re= /^[0-9]*$/; if(!re.test(vcode)) { vcodeflag = 1; $(#vcodemsg).html(请正确输入验证码); return false; }else { vcodeflag = 0; $(#vcodemsg).html(); }}$(document).ready(function(){/*** 修改用户信息验证 begini */// begin email$(#email).focus(function() { check_email();});// 邮箱格式判断$(#email).blur(function(){check_email();});//end email//bengin realname$(#realname).focus(function() {check_realname();});// 校验realname是否正确$(#realname).blur(function() {check_realname();});//end realname//bengin cellphone$(#cellphone).focus(function() { check_cellphone(); });// 手机号码格式判断$(#cellphone).blur(function() { check_cellphone(); });//end cellphone//bengin homephone$(#homephone).focus(function(){ check_homephone(); });$(#homephone).blur(function(){ check_homephone();});//end homephone//bengin addr// 校验address是否正确$(#address).focus(function(){ check_address(); });$(#address).blur(function() { check_address();});//end addr//bengin zip$(#zip).focus(function(){ check_zip(); });$(#zip).blur(function(){ check_zip(); });//end zip//获取市级$(#province).change(function(){ var province=$(this).val(); $.post(_jquery, {type:getprovince , province:province}, function(msg){ $(#city).html(msg); $(#region).html(); });});//获取县级$(#city).change(function(){ var city=$(this).val(); $.post(_jquery, {type:getcity , city:city}, function(msg){ $(#region).html(msg); });});/*** 修改用户信息验证 end ***********//***------密码修改验证 begin *************///begin oldpassword$(#oldpassword).focus(function(){ check_oldpassword();});$(#oldpassword).blur(function(){ check_oldpassword();});//end oldpassword//begin newpassword$(#newpassword).focus(function(){ check_newpassword();});//newpassword$(#newpassword).blur(function(){ check_newpassword();});// repeatpassword$(#repeatpassword).focus(function(){ check_repeatpassword();});$(#repeatpassword).blur(function(){ check_repeatpassword();});/***------密码修改验证 end *************//**** ---------卡查询表单提交验证 begin ***********/var cardnumflag = 0;var passwordflag = 0;var vcodeflag = 0; $(#cardnum).focus(function(){ check_cardnum();});$(#cardnum).blur(function(){ check_cardnum();});$(#password).focus(function(){ check_password();});$(#password).blur(function(){ check_password();});$(#vcode).focus(function(){ check_vcode(); }); $(#vcode).blur(function(){ check_vcode(); });$(#formsubmit).click(function(){ $(#strmsg).hide(); $(#nummsg).html(); $(#psmsg).html(); $(#vcodemsg).html(); var re= /^[0-9]*$/; if(cardnumflag != 0 || $.trim($(#cardnum).val()) == || $(#cardnum).val() == null ) { $(#cardnum).focus(); return ; } if(passwordflag != 0 || $.trim($(#password).val()) == || $(#password).val() == null) { $(#password).focus(); return ; } if(($(#vcode).val() != && !re.test($(#vcode).val())) || vcodeflag != 0 || $(#vcode).val() == ) { $(#vcode).focus(); return ; } $(#cardform).submit();});/**** ---------卡查询表单提交验证 end ***********///用户订单取消,$(#cancelreason).focus(function(){ $(#errorinfo).html(); $(#errorinfo).removeclass(tips);});$(#cancelreason).bind('input propertychange', function() { var maxlength = 65; if ($(this).val().length > maxlength) { $(this).val($(this).val().substring(0, maxlength)); } return false;})//-----});//end ready//修改用户信息表单提交function formsubmit(){ $(#showspan).hide(); var province=$(#province).val(); var city=$(#city).val(); var region=$(#region).val(); $(#emailmsg).html(); $(#realnamemsg).html(); $(#cellphonemsg).html(); $(#homephonemsg).html(); $(#szcode).html(); $(#addressmsg).html(); $(#zipmsg).html(); if(emailflag != 0 || $(#email).val()== null || $(#email).val()== ) { $(#email).focus(); return ; } if(realnameflag != 0 || $(#realname).val()== null || $(#realname).val()== ) { $(#realname).focus(); return ; } if($(input[name=sex]:checked).val()==null||$(input[name=sex]:checked).val()==){ alert(请选择性别); $(#sex).focus(); return; } if(cellphoneflag != 0) { $(#cellphone).focus(); return ; } if(homephoneflag != 0) { $(#homephone).focus(); return ; } if( province == || city == || region== || province == null || city == null || region== null) { $(#szcode).html(请选择地区); return ; } if(addressflag != 0 || $(#address).val() == null || $(#address).val() == ) { $(#address).focus(); return ; } if(zipflag != 0) { $(#zip).focus(); return ; } $(#myform).submit();}//修改用户信息表单重置function formreset(){ $(#showspan).hide(); document.forms[myform].reset(); $(#emailmsg).html(); $(#realnamemsg).html(); $(#cellphonemsg).html(); $(#homephonemsg).html(); $(#addressmsg).html(); $(#zipmsg).html(); $(#szcode).html(); $(#province).html($(#hprovince).val()); $(#city).html($(#hcity).val()); $(#region).html($(#hregion).val());}//修改密码提交formfunction psformsubmit(){ $(#oldpsmsg).html();; $(#newpsmsg).html(); $(#repsmsg).html(); var oldps = $.trim($(#oldpassword).val()); var newps = $.trim( $(#newpassword).val()); var reps = $.trim($(#repeatpassword).val()); if( oldpasswordflag != 0 || oldps == || oldps ==null ) { $(#oldpassword).focus(); return ; } if(newpasswordflag != 0 || newps == || newps ==null ) { $(#newpassword).focus(); return ; } if( newpasswordflag != 0 || reps == null || reps == || reps != newps) { $(#repeatpassword).focus(); return ; } $(#psform).submit();}//获取验证码function dochange(){ ///$(#vcodeimg).attr(src,_verifycode? + new date()); var imgobj=#vcodeimg; $(imgobj).fadeout('fast', function(){ var datenow = new date(); $(this).attr('src', '_verifycode?ver=' + datenow.getmilliseconds()).delay(200).fadein('slow'); });}function removefavorite(goodsid){ if(confirm('确定删除商品吗?')) { document.location.href=/myremovefavorite.html?goodsid=+goodsid; }}function selectprovince(value){ //导出省份下市区 信息。 $.post(_jquery, {type:getprovince,province:value}, function(data){ $(#city).html(data); $(#region).html(); });}function selectcity(value){ //导出市区 下县城信息。 $.post(_jquery, {type:getcity,city:value}, function(data){ $(#region).html(data); });}
希望本文所述对大家的jquery程序设计有所帮助。