jQuery表单验证

登录验证:
$(function(){
$("#文本框id").focus(function(){ // 输入账号的文本框获得鼠标焦点
var name = $(this).val(); // 得到当前文本框的值
if(name=="请输入6~12位账号"){
$(this).val(""); // 如果符合条件,则清空文本框内容
}
});
$("#文本框id").blur(function(){// 文本框失去鼠标焦点
var name = $(this).val(); // 得到当前文本框的值
if(name==""){
$(this).val("请输入6~12位账号");// 如果符合条件,则设置内容
}
});
});
表单验证:
$(function(){
$("#txtNo").blur(function(){
var name=$(this).val();
if(name==""||name==null)
{
$("#prompt_no").html("用户账号不能为空!");
return false;
}
if(name.length<6||name.length>12)
{
$("#prompt_no").html("账号的长度在6到12位之间!");
return false;
}
$("#prompt_no").html("<img src='images/ok.gif'/>");
});
$("#txtPwd").blur(function(){
var pwd=$(this).val()
if(pwd==""||pwd==null)
{
$("#prompt_pwd").html("密码不能为空!");
return false;
}
if(pwd.length<6||pwd.length>12)
{
$("#prompt_pwd").html("密码的长度在6到12位之间");
return false;
}
$("#prompt_pwd").html("<img src='images/ok.gif'/>");
});
$("#txtConfirmPwd").blur(function(){
var pwds=$(this).val();
if(pwds==""||pwds==null){
$("#prompt_confirmpwd").html("密码不能为空!");
return false;
}
if(pwds!=$("#txtPwd").val())
{
$("#prompt_confirmpwd").html("两次输入的密码要一致!");
return false;
}
$("#prompt_confirmpwd").html("<img src='images/ok.gif'/>");
});

$("#txtName").blur(function(){
var names=$(this).val();
if(names==""||names==null){
$("#prompt_name").html("用户名不能为空!");
return false;
}
if(names.length<6||names.length>12)
{
$("#prompt_name").html("用户名的长度在6到12位之间!");
return false;
}
$("#prompt_name").html("<img src='images/ok.gif'/>");

});
$("#txtId").blur(function(){
var idReg=/^\d{15}$|^\d{18}$/;
var id=$(this).val();
if(id==""||id==null)
{
$("#prompt_id").html("身份证不能为空!");
return false;
}
if(!idReg.test(id))
{
$("#prompt_id").html("身份证格式不正确!");
return false;
}
$("#prompt_no").html("<img src='images/ok.gif'/>");
});

$("#txtPhone").blur(function(){
var phoneReg=/^(13|15|18)\d{9}$/;
var phone=$(this).val();
if(phone==""||phone==null)
{
$("#prompt_phone").html("手机号不能为空!");
return false;
}
if(!phoneReg.test(phone))
{
$("#prompt_phone").html("手机号格式不正确!");
return false;
}
$("#prompt_phone").html("<img src='images/ok.gif'/>");
});

 

posted @ 2015-03-19 18:08  酸酸の柚子  阅读(472)  评论(0编辑  收藏  举报