前端jquery即时验证问题

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<div>请输入邮箱:<input id="email" type="text"><span id="emailInfo"></span></div>
<div>请输入密码:<input id="pwd" type="password"><span id="pwdInfo"></span></div>
<button type="button">提交</button>
</body>
<script src="jquery.min.js"></script>
<script>
$(function(){
    //对获取数据进行简单验证(这不是重点)
    var em = /\w+@\w+\.\w+/;
    //当失去焦点时触发事件
    $('#email').on('blur', function () {
        if(em.test($(this).val())){
            $('#emailInfo').html('正确').css('color','green');
        }else{
            $('#emailInfo').html('错误').css('color','red');
	//输入错误时不能脱离焦点
            $(this).focus();
        }
    });


    $('#pwd').on('blur', function () {
        var p = $(this).val();
        if(p.length <= 16 && p.length >= 6){
            $('#pwdInfo').html('正确').css('color','green');
        }else{
            $('#pwdInfo').html('错误').css('color','red');
            $(this).focus();
        }
    });


    $('button').on('click',function(){
        $('#pwdInfo').html('');
        $('#emailInfo').html('');
	//通过ajax与后端进行交互,在规定好的规则内返回既定的值
        $.get('server.php',{'email':$('#email').val(),'pwd':$('#pwd').val()},function(e){
            if(e == 0){
                $('#emailInfo').html('请输入正确的邮箱').css('color','red');
                $('#email').focus();
            }

            if(e == 1){
                $('#pwdInfo').html('请输入正确的密码').css('color','red');
                $('#pwd').focus();
            }

            if(e == 2){
                alert('成功');
            }
        })
    })

})
</script>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值