Javascript 实现基础表单项检查

<script>
    //注册用户名规则检查
    function checkusername(username){
        if (username.length > 64 || username.length < 8) {
            layer.msg('用户名长度有误,请核实后在输入');
            return false;
        }
        return true;
    }
    //注册密码规则检查
    function checkpassword(password){
        var strCheck = /^[a-zA-Z0-9_]{1,}$/;
        if (!password.match(strCheck)) {
            layer.msg('密码只能由字母数字下划线组成');
            return false;
        }
        if (password.length > 128 || password.length < 8) {
            layer.msg('密码长度有误,请核实后在输入');
            return false;
        }
        var strCheckf = /^[a-zA-Z]{1,}$/;
        var ref = password.substring(0, 1);
        if (!ref.match(strCheckf)) {
            layer.msg('密码首字符应为字母,请核实后输入');
            return false;
        }
        return true;
    }
    //注册邮箱验证
    function checkemail(email){
        var strCheck = /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/;
        if (!email.match(strCheck)) {
            layer.msg('邮箱不合法');
            return false;
        }
        return true;
    }
    //手机号验证
    function checkephone(phone){
        var strCheck = /^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/;
        if (!phone.match(strCheck)) {
            layer.msg('手机号不合法');
            return false;
        }
        return true;
    }
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer;
    
        // 进行注册操作
        form.on('submit(register)', function (data) {
            data = data.field;
            if(checkusername(data.username) && checkpassword(data.upassword) && checkemail(data.uemail) && checkephone(data.uphone)){
       
                $.post("/Logintest/RegisterServlet", data, function (result){
                    if(result.search("注册成功") == -1){
                        layer.msg(result, function () {
                            return false;
                        });
                    }else {
                        layer.msg(result, function () {
                            window.location = './login-1.html';
                        });
                    }

                });
                return false;
            }
            return false;
        });

    });

</script>

 

阅读剩余
THE END