AJAX提交之前的JavaScript验证

时间:2017-08-30 14:55:21

标签: javascript jquery ajax

我在允许提交ajax之前尝试验证表单,但是我有两个问题。第一个问题是我不知道在提交之前如何最好地进行验证(最专业的流程)。第二个问题是,什么阻止了我目前工作的验证码?希望提高效率,欢迎所有投入。非常感谢。



 $('#form-reg').on('submit', function(){

     var bool = false;
     var name = document.getElementById('#name-reg');
     var email = document.getElementById('#email-reg');

     console.log(name);
     console.log(email);
     if(!/[^a-zA-Z]/.test(name)){
       bool = true;
     }
     else{
       bool = false;
     }
     if(bool == true){
        console.log(document.getElementById('#name-reg'));
        $('#form-reg').slideUp('slow');
        // serialize the form
        var formData = $(this).serialize();
        console.log(formData);
        $.ajax({
            type        : 'POST',
            url         : 'register.php',
            data        : formData,

            success: function() {
              alert("Success");
            },
             error: function(xhr) {
              alert("fail");
            }
        })
        .done(function (data) {
              document.getElementById('form-reg').reset();

        })
        .fail(function (error) {
            alert("POST failed");
        });
        //return false;
      }
      else {
        alert('try again');
      }
  });

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js" type="text/javascript">

  <form id = "form-reg">
      <label id ="x" for="name">Name</label>
      <input id="name-reg" name="name"></br>
      <label id = "y" for="email">Email</label>
      <input id="email-reg" name="email"></br>
      <input type="submit" value="submit" id = "submit-reg">
    </form>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

您的代码存在问题,您必须停止提交表单,如:

 for i in $(seq 0 0.05 1); do awk -v i=$i '{if ($7 <= i) print $0}' file.txt | wc -l ; done

并在检查验证后,如果验证成功,则提交表单。

答案 1 :(得分:0)

虽然这是一个小答案,但无论如何我都会发布。问题看起来像是

的使用
  

var name = document.getElementById('#name-reg');

#导致第一个问题,应该是

  

var name = $('#name-reg')。val();

$('#form-reg').on('submit', function(){

     var bool = false;
     var name = $('#name-reg').val();
     var email = $('#email-reg').val();

     console.log(name);
     console.log(email);
     if(!/[^a-zA-Z]/.test(name)){
       bool = true;
     }
     else{
       bool = false;
     }
     if(bool == true){
        console.log($('#name-reg').val());
        $('#form-reg').slideUp('slow');
        // serialize the form
        var formData = $(this).serialize();
        console.log(formData);
        $.ajax({
            type        : 'POST',
            url         : 'register.php',
            data        : formData,

            success: function() {
              alert("Success");
            },
             error: function(xhr) {
              alert("fail");
            }
        })
        .done(function (data) {
              document.getElementById('form-reg').reset();

        })
        .fail(function (error) {
            alert("POST failed");
        });
        //return false;
      }
      else {
        alert('try again');
      }
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js" type="text/javascript">

  <form id = "form-reg">
      <label id ="x" for="name">Name</label>
      <input id="name-reg" name="name"></br>
      <label id = "y" for="email">Email</label>
      <input id="email-reg" name="email"></br>
      <input type="submit" value="submit" id = "submit-reg">
    </form>