TEL:400-8793-956
当前位置:程序、服务器

防止在Jquery中提交表单?

提问者: 近期获赞: 浏览人数: 发布时间:2021-01-06 13:10:37

 问:如果下一个我如何防止提交:-

用户名太短
第一个字符必须没有字母
user_name
无效电子邮件格式
电子邮件不可用
密码不匹配
 
添加和删​​除一些代码行后,我遇到了其他问题,密码确认不起作用
 
 
以及登录登录页面后如何重定向
 
$(document).ready(function() {
     $("#user_name").keyup(function() {
        var user_name = $("#user_name").val();
        if (user_name == "") {
            $("#username_error").html(" ");
            
        } 
         else if (user_name.length < 3) {
            $("#username_error").html("to short");
            
        }else if(validate_user_name(user_name)){
            $("#username_error").html("first character must be a letter");
        }
        
         else {
            
            $.ajax({
                type : "POST",
                url : "processes.php",
                data : "user_name=" + user_name,
                success : function(data) {
                    $("#username_error").html(data);
                    $("#loaderIcon").hide();
                },
                
            });
        }
        
            function validate_user_name(user_name) {
              var filter = /^[a-z](?:_?[a-z0-9]+)*$/i;
                if (!filter.test(user_name)) {
                    
                 return true;
                 
                } else {
                    
                 return false;
                
                }
            }
        
    });
    $("#myemail").keyup(function() {
        var myemail = $(this).val();
        if (myemail == "") {
            $("#email_error").html("fill out your email");
        }
        
        else if(validate_myemail(myemail)){
            $("#email_error").html("Invalid Email Address");
        }
        else {
            $.ajax({
                type : "POST",
                url : "processes.php",
                data : "myemail=" + myemail,
                success : function(msg) {
                    
                        $("#email_error").html(msg);
                }
            });
        }
        
    function validate_myemail(myemail) {
    var filter = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
    if (!filter.test(myemail)) {
        return true;
    } else {
        return false;
    }
}
        
    });
    $("#mypassword").keyup(function() {
        var mypassword = $("#mypassword").val();
        $('#password_error').html(checkStrength($('#mypassword').val()))
        if (mypassword == "") {
            $("#password_error").html(" ");
    
        } 
        
    function checkStrength(mypassword)
    {
        
        var strength = 0
        
        
        if (mypassword.length < 3) { 
            $('#password_error').removeClass()
            $('#password_error').addClass('short')
            return 'Too short' 
        }
        
    
        if (mypassword.length > 7) strength += 1
        
        
        if (mypassword.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))  strength += 1
        
        
        if (mypassword.match(/([a-zA-Z])/) && mypassword.match(/([0-9])/))  strength += 1 
        
    
        if (mypassword.match(/([!,%,&,@,#,$,^,*,?,_,~])/))  strength += 1
        
        
        if (mypassword.match(/(.*[!,%,&,@,#,$,^,*,?,_,~].*[!,%,&,@,#,$,^,*,?,_,~])/)) strength += 1
        
        if (strength < 2 )
        {
            $('#password_error').removeClass()
            $('#password_error').addClass('weak')
            return 'Weak'            
        }
        else if (strength == 2 )
        {
            $('#password_error').removeClass()
            $('#password_error').addClass('good')
            return 'Good'        
        }
        else
        {
            $('#password_error').removeClass()
            $('#password_error').addClass('strong')
            return 'Strong'
        }
    }
        
    });
    $("#mypasswordconfirm").keyup(function() {
        var mypasswordconfirm = $("#mypasswordconfirm").val();
        if (mypasswordconfirm == "") {
            $("#passwordconfirm_error").html(" ");
        
        } else if (mypassword !== mypasswordconfirm) {
            
            $("#passwordconfirm_error").html("no match");
            
        } else {
            $("#passwordconfirm_error").html("great");
            
        }
    });
    $("#mysubmit").click(function(prevent) {
        if ($.trim($("#user_name").val()) === "" || $.trim($("#myemail").val()) === "" || $.trim($("#mypassword").val()) === "" || $.trim($("#mypasswordconfirm").val()) === "") {
            prevent.preventDefault();
            $("#mysubmit_error").html("please fill out all the form data");
            return false;
        } else {
            $.ajax({
                type : "POST",
                url : "processes.php",
                data : "username" + user_name + "&myemail" + myemail + "&mypassword" + mypassword,
                success : function(message) {
                    if (message == "done") {
                        $("#mysubmit_error").html("you are signed up!");
                    } else {
                        $("#mysubmit_error").html("you are not signed up!!");
                    }
                }
            });
        }
    });
    
     
});
 
 
 
答:我检查了您的问题,发现您没有使用查询的Submit事件。您可以为用户名和电子邮件等不同字段输入不同的条件。如果没有正确填写,则使用reutrn false或preventDefault停止表单。或者您可以使用jquery.validation.js lib https://jqueryvalidation.org/谢谢
上一篇: 最新的链接构建技术是什么?
下一篇: 如何使用swingworker从JTable删除行?