如何检查函数是否返回true

时间:2013-10-13 04:49:30

标签: javascript jquery

我有一个Web表单提交,如果我的函数validate form在该函数中返回true我写了一条if语句,如果另一个名为usernamecheck的函数返回true,则返回validateform函数true。除非您单击按钮检查用户名,否则我不希望表单提交。我知道我没有写这个我希望你理解的最佳方式

<!-- Signup Form -->

  <form name='signup' action="subscription.php" onsubmit="return validateForm();" method="post" >


    <input type="text" id="signupUsername" name="signupusername" placeholder="Business Name" tabindex=1 required>

    <input type="password" id="signupPassword" placeholder="Password" name="signuppassword" tabindex=2 required> <br>

    <input type="text" id="ownerName" placeholder="Owner's Name" name="ownername" tabindex=3 required>

    <input type="email" id="signupEmail" placeholder="Email" name="signupemail" tabindex=4 required> 

    <input type="tel" id="signupphoneNumber" placeholder="Phone Number" name="signupphonenumber" tabindex=5 required>

    <input type="image" id="signupSubmit" src="images/signupBtn.jpg">

    <input type="text" id="city" placeholder="City" name="city" tabindex=6>

    <input type="text" id="state" placeholder="State" name="state" tabindex=7>

这是您单击以检查用户名是否存在的按钮

    <input type="button" id='check' value="Check It">

//

  </form>

  <script type="text/javascript">

下面是函数,如果单击上面的按钮,它会检查函数usernamecheck

 $(function() {
 $( "#check" ).click(function() {
 return usernamecheck();
 });
 });

下面是validateForm函数,如果usernamecheck返回true,它也返回true并提交表单

function validateForm()
{

  if(usernamecheck() && $("#signupUsername").val().length < 4) {

    return true;

  }

}


function usernamecheck() {

 $.post( "checkusername.php", { username: $("#signupUsername").val() })
  .done(function( data ) {

     result = JSON.parse(data);


      if(result["status"]== "Username is taken") 

  {

          alert("username is taken");
          return false;          

  }

  else if(result["status"]== "Username is Available") {

    alert("username is Available");
    return true;

}

else {

alert('You did not check the username');

}

});

}



</script>



<!-- Map Logo -->
<img src='images/map.jpg' id="map" class='menuitems'>
<!-- About Us Logo -->
<img src='images/aboutus.jpg' id="aboutus" class='menuitems'>
<!-- People Logo -->
<img src='images/people.jpg' id="people" class='menuitems'>

  </div>    

  </div>
 </div>


</body>

</html>

0 个答案:

没有答案