Django模板中的AJAX表单验证

时间:2018-09-16 14:15:22

标签: jquery ajax django

我正在django模板项目中练习AJAX,我有一个注册表单,其中有4个字段要提交,我正在使用注册按钮功能来实现ajax功能,它的工作正常,但是问题是当我保持表单为空时并点击其提交的按钮,那么我该如何验证字段,当没有给出任何值时,它将显示错误消息,以填满所有字段?提前致意。

    <script>
    $(document).ready(function(){
         $('#signup-btn').click(function(event){
            console.log('hi-signup')

            $.ajax({
                method: "POST",
                url: '/register',
                data: {
                    name :$('#id_fullname').val(),
                    email : $('#id_email').val(),
                    country : $('#id_country').val(),
                    password : $('#id_password').val(),
                    csrfmiddlewaretoken:'{{ csrf_token }}',

                },

                success: function(res) {
                var response = $.parseJSON(res)
                $('.signup-data').html(response.msg)
                          if (response.code == 200) {
                            $('.signup-data').html(response.msg);
                            window.location = "http://localhost:8000";
                        }
                        },
            })

        })

    })
</script>

html

  <form  class="my-signup-form" action="/register" method="post">
                                  {% csrf_token %}
                                  <div class="signup-data"></div>
                                  <div class="top-row">
                                    <div class="field-wrap">
                                      <input name="signup-fullname" id="id_fullname" type="text" required autocomplete="off" placeholder="Full Name"/>
                                    </div>

                                    <div class="field-wrap">
                                      <input name="signup-email" id="id_email" type="text"required autocomplete="off" placeholder="Email ID"/>
                                    </div>
                                  </div>

                                  <div class="field-wrap select2">
                                    <span class="select-arrow"></span>
                                    <select name="signup-country" id="id_country" class="selextbox">
                                     <option value="" selected disabled>Select your country</option>
                                    <option value="United States">United States</option>
                                    <option value="United Kingdom">United Kingdom</option>
                                    <option value="Afghanistan">Afghanistan</option>

                                </select>
                                  </div>


                                  <div class="field-wrap">
                                    <input name="signup-password" id="id_password" type="password"required autocomplete="off" placeholder="Password"/>
                                  </div>
                                  <p class="charcters">Minimum 8 Charcters</p>

                                  <button id="signup-btn" type="button" class="button button-block"/>Get Started</button>

                              </form>

2 个答案:

答案 0 :(得分:1)

<script>
            $('#signup-btn').click(function(event){
                if(validate()){
                    $.ajax({
                        method: "POST",
                        url: '/register',
                        data: {
                            name :$('#id_fullname').val(),
                            email : $('#id_email').val(),
                            country : $('#id_country').val(),
                            password : $('#id_password').val(),
                            csrfmiddlewaretoken:'{{ csrf_token }}',

                        },
                        success: function(res) {
                            var response = $.parseJSON(res)
                            $('.signup-data').html(response.msg)
                                  if (response.code == 200) {
                                  $('.signup-data').html(response.msg);
                                  window.location = "http://localhost:8000";
                            }
                         },
                    })

                  })
                }

            })

            function validate(){

                var isValid = true;
                if (!$('#id_fullname').val()){
                    isValid = false
                }
                if (!$('#id_email').val()){
                    isValid = false
                }else{
                   if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test($('#id_email').val()))){
                        isValid = false; 
                    }
                }
                if (!$('#id_country').val()){
                    isValid = false
                }
                if (!$('#id_password').val()){
                    isValid = false
                }
                return isValid;
            }
</script>

答案 1 :(得分:1)

我记得这是我以前在我的一个应用程序尝试中所做的:

$(document).ready(function(){

$('#signup-btn').click(function(){

    formValidate();   

     console.log('hi-signup')

            $.ajax({
                method: "POST",
                url: '/register',
                data: {
                    name :$('#id_fullname').val(),
                    email : $('#id_email').val(),
                    country : $('#id_country').val(),
                    password : $('#id_password').val(),
                    csrfmiddlewaretoken:'{{ csrf_token }}',

                },

                success: function(res) {
                var response = $.parseJSON(res)
                $('.signup-data').html(response.msg)
                          if (response.code == 200) {
                            $('.signup-data').html(response.msg);
                            window.location = "http://localhost:8000";
                        }
                        },
            })

        })

function formValidate(){

    var name = $('#id_fullname').val();
    var email = $('#id_email').val();

    var country = $('#id_country').val();
    var password = $('#id_password').val();

    var nameReg = /^[A-Za-z]+$/;
    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

    var inputValue = new Array(name, email, country, password);

    var inputMessage = new Array("name", "email", "country", "password");

     $('.error-message').hide();

        if(inputValue[0] == ""){
            $('#id_fullname').after('<span class="error-message"> Please enter your ' + inputMessage[0] + '</span>');
        } 
        else if(!nameReg.test(name)){
            $('#id_fullname').after('<span class="error-message"> Letters only</span>');
        }

        if(inputVal[1] == ""){
            $('#id_email').after('<span class="error-message"> Please enter your ' + inputMessage[1] + '</span>');
        } 
        else if(!emailReg.test(email)){
            $('#id_email').after('<span class="error-message"> Please enter a valid email address</span>');
        }

        if(inputVal[2] == ""){
            $('#id_country').after('<span class="error-message"> Please enter your ' + inputMessage[2] + '</span>');
        } 

        if(inputVal[3] == ""){
            $('#id_password').after('<span class="error-message"> Please enter your ' + inputMessage[3] + '</span>');
        }       
}   

});