0

好的,所以我正在尝试使用 jQuery 进行表单验证。不知道为什么,但是当您单击提交时,它会将您重定向到您已经在的页面。

表单操作是空白的,我在标题中包含了 javascript 文件。

我将把代码放在下面。如果您看到有什么问题,请告诉我。我希望它使用 jQuery 进行验证,然后将您发送到一个 php 文件,其中包含方法 POST 中的这些值。

注册.js:

$(document).ready(function() {
  $('button[name=regsubmit]').click(function(){
    var fname = $('input[name=regfirstname');
    var lname = $('input[name=reglastnaame');
    var email = $('input[name=regemail');
    var password = $('input[name=regpassword');
    var repeatpassword = $('input[name=regrepeatpassword');
    var username = $('input[name=regusername');
    var atpos = email.indexOf("@");
    var dotpos = email.indexOf(".");
    if (fname==null || fname=="")
      {
      alert('First name must be filled out!');
      return false;
      }
    if (lname==null || lname=="")
      {
      alert('Last name must be filled out!');
      return false;
      }
    if (atpos<1 || dotpos<atpos+2 || dotpos+2>=email.length)
      {
      alert('Not a valid e-mail address!');
      return false;
      }
    if (username==null || username=="")
      {
      alert("Username field must be filled out!");
      return false;
      }
    if (password==null || password=="")
      {
      alert("Subject field must be filled out!");
      return false;
      }
     if (repeatpassword==null || repeatpassword=="")
      {
      alert("Repeat password field must be filled out!");
      return false;
      }
    if (password != repeatpassword)
      {
      alert("The passwords do not match!");
      return false;
      }
    else
    {
    location.href="register.php";
    }
  });
});

形式:

               <form name="register" method="POST">
                <fieldset>
                  <div class="form-container row-fluid">
                    <div class="span6"> 

                      <div class="control-group">
                        <label class="control-label" for="regfirstname">First Name</label>
                        <div class="controls">
                          <input id="regfirstname" tabindex="1" name="regfirstname" type="text" placeholder="First Name" class="input-large" required>
                        </div>
                      </div>

                      <div class="control-group">
                        <label class="control-label" for="regusername">Username</label>
                        <div class="controls">
                          <input id="regusername" tabindex="3" name="regusername" type="text" placeholder="Username" class="input-large" required>
                        </div>
                      </div>



                      <div class="control-group">
                        <label class="control-label" for="regpassword">Password</label>
                        <div class="controls">
                          <input id="regpassword" tabindex="5" name="regpassword" type="password" placeholder="Password" class="input-large">
                        </div>
                      </div>
                          <div class="controls">
                            <button id="regsubmit" tabindex="7" name="regsubmit" class="button background-asbestos">Submit</button>
                          </div>
                    </div><!-- .span6 -->

                    <div class="span6"> 

                      <div class="control-group">
                        <label class="control-label" for="reglastname">Lastname</label>
                        <div class="controls">
                          <input id="reglastname" tabindex="2" name="reglastname" type="text" placeholder="Lastname" class="input-large" required>
                        </div>
                      </div>

                      <div class="control-group">
                        <label class="control-label" for="regemail">Email</label>
                        <div class="controls">
                          <input id="regemail" name="regemail" tabindex="4" type="text" placeholder="example@example.com" class="input-large"</td>
                        </div>
                      </div>

                      <div class="control-group">
                        <label class="control-label" for="regrepeatpassword">Repeat Password</label>
                        <div class="controls">
                          <input id="regrepeatpassword" tabindex="6" name="regrepeatpassword" type="password" placeholder="Repeat Password" class="input-large" required>
                        </div>
                      </div>
                      <!-- .span6 -->
                    <!-- span6 --> 
                  </div><!-- .row-fluid -->
                </fieldset>
              </form>
4

2 回答 2

1

好吧,你的 JS 和 HTML 是一堆乱七八糟的东西,所以我不会全部重新创建,而是基础:

为您的表单提供一个 ID:

<form id="SomeForm" name="register" method="POST">

然后阻止默认操作,如果通过则提交:

// reference the button by the ID you gave it
$('#regsubmit').click(function(e){
    // stop original submission
    e.preventDefault();

    // all your checking stuff here, but if true, then:
    document.getElementById('SomeForm').submit();
});

这可确保您正确引用按钮,防止正确提交,并且仅在有效时提交。

于 2013-10-22T20:31:05.313 回答
0

防止提交时的默认操作

$('button[name=regsubmit]').click(function(e)
{
  e.preventDefault();

 //your code below    

}
于 2013-10-22T20:30:47.683 回答