简体   繁体   中英

Validation form using Jquery

I am doing client side validation to my registration page in html by using jquery. After submitted the form without giving text entries, I am getting the alert box with proper info what I have given in jquery function. But the problem is after click on "OK" button on alert box, the form is going to submit to action page directly. Can any one please help on it. Thanks, Swami.

<script>
function Submit(){
var fname = $("#fname").val();
if($("#fname").val() == "" ){
alert("please enter firsname")
return false;
};
};
</script>
<form method="post" action="#">
//fields here
<input type="button" value="Register" onclick="Submit();">
</form>

Edit: Well, I rewrote my code snippet - it should work now.

<script>
function validateForm() {
  var fname = $("#fname").val();
  if(fname === "")
  {
    alert("Please enter your name!");
    return false;
  } else {
    $("#formid").submit();
    return true;
  }
}
</script>
<form id="formid" method="post" action="#">
  <input type="text" id="fname">
  <input type="button" value="Register" onclick="return validateForm();">
</form>

Have a look at this: JavaScript code to stop form submission .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM