一聚教程网:一个值得你收藏的教程网站

热门教程

Jquery常见表单验证(手机、邮箱、焦点)

时间:2022-11-14 22:03:15 编辑:袖梨 来源:一聚教程网

1、EMAIL:

代码如下 复制代码

function emailFormatCheck(email){
if ((email.length > 128) || (email.length < 6)) {
return false;
}
var format = /^[A-Za-z0-9+]+[A-Za-z0-9._-+]*@([A-Za-z0-9-]+.)+[A-Za-z0-9]+$/;
if (!email.match(format)) {
return false;
}
return true;
}

2、手机号:

代码如下 复制代码

function mobilephoneFormatCheck(mobilephone){
var format = /^0?(13[0-9]|15[012356789]|18[0236789]|14[57])[0-9]{8}$/;
if (!mobilephone.match(format)) {
return false;
}
return true;
}

失去焦点判断加按钮判断(晚点整理注释):

代码如下 复制代码

$("#username").blur(function(){
if(!nameFormatCheck($("#username").val()))
{
$("#username").siblings("p").html('请输入全中文姓名').show();
}
else
{
$("#username").siblings("p").html('').show();
}
});
$("#youxiang").blur(function(){
if(!emailFormatCheck($("#youxiang").val()))
{
$("#youxiang").siblings("p").html('请输入正确的邮箱地址如:username@lvmama.com').show();
}
else
{
$("#youxiang").siblings("p").html('').show();
}
});
$("#shouji").blur(function(){
if(!mobilephoneFormatCheck($("#shouji").val()))
{
$("#shouji").siblings("p").html('请输入正确的手机号如:13800000000').show();
}
else
{
$("#shouji").siblings("p").html('').show();
}
});
$('.submit').click(function(){
if(!nameFormatCheck($("#username").val()))
{
$("#username").siblings("p").html('请输入全中文姓名').show();
$("#username").focus();
}
else if(!emailFormatCheck($("#youxiang").val()))
{
$("#youxiang").siblings("p").html('请输入正确的邮箱地址如:username@lvmama.com').show();
$("#youxiang").focus();
}
else if(!mobilephoneFormatCheck($("#shouji").val()))
{
$("#shouji").siblings("p").html('请输入正确的手机号如:13800000000').show();
$("#shouji").focus();
}
else
{
var leftN=$(window).width()/2-100;
var topN=$(window).height()/2-100;
$('.bg-tan').show();
$(".tcc").show().css({"top":$(window).scrollTop()+300,"left":leftN});
}

})

热门栏目