Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stopping form submit if the validation fails.

I am validating the dates in below function. If the validation fails, then the form should not get submitted. I tried returning false in form onsubmit but it still gets submitted. However, Validation is working fine and getting the alert that I put in the function. Any help to stop submitting the form if validation fails.

<script>
  function dateCheck()
  {
      start = document.getElementById('name3').value;
      end = document.getElementById('name4').value;
      compare(start, end);
      document.getElementById('name4').focus();

  }

  function compare(sDate, eDate)
  {

      function parseDate(input) {
            var parts = input.match(/(\d+)/g);

            return new Date(parts[2], parts[0]-1, parts[1]); // months are 0-based
          }
       var parse_sDate = parseDate(sDate);
      var parse_eDate = parseDate(eDate);
       parse_sDate.setFullYear(parse_sDate.getFullYear() + 1);

      if(parse_eDate >= parse_sDate)
          {
          alert("End date should not be greater than one year from start date");
           return false;
          }
       return true;
  }

</script>
</head>
<body>
<form onsubmit="return dateCheck()">
<table>
<tr>
<td><input type="text" name="soname3" id="name3" size="15" readonly="readonly"> 
<img src="../Image/cal.gif" id="" style="cursor: pointer;" onclick="javascript:NewCssCal('name3','MMddyyyy','dropdown',false,'12')" /></td>
 <td><input type="text" name="soname4" id="name4" size="15" readonly="readonly">
 <img src="../Image/cal.gif" id="" style="cursor: pointer;" onclick="javascript:NewCssCal('name4','MMddyyyy','dropdown',false,'12'); " /> </td>
 </tr>
</table>
<input type="submit" value="Submit">
</form>
like image 671
user2581072 Avatar asked Jul 15 '13 03:07

user2581072


People also ask

How do you stop form submit if the validation fails?

Use the return value of the function to stop the execution of a form in JavaScript. False would return if the form fails to submit.

How do I stop form submitting?

The simplest solution to prevent the form submission is to return false on submit event handler defined using the onsubmit property in the HTML <form> element.

Why is it important to validate forms before sending?

Why is Form Validation Needed? Form validation is required to prevent online form abuse by malicious users. Improper validation of form data is one of the main causes of security vulnerabilities. It exposes your website to attacks such as header injections, cross-site scripting, and SQL injections.

How Stop form submit in MVC?

Handle the forms . submit() event and cancel it if necessary.


2 Answers

Just a comment:

If your listener passes a reference to the form, you can access the controls by name or ID:

<form onsubmit="return dateCheck(this)">

then:

function dateCheck(form) {
  var start = form.name3.value;
  ...
}

Note that you should declare variables, otherwise they will become global at the point they are assigned to. Also, you should check the values in the controls before passing them to the compare function (and display a message asking the user to enter a valid value if they aren't).

function dateCheck(form) {
  var start = form.name3.value;
  var end = form.name4.value;
  var valid = compare(start, end);

  if (!valid) form.name4.focus();

  return false;
}
like image 82
RobG Avatar answered Sep 27 '22 19:09

RobG


I appreciate all contributions above. I have just applied the suggestions above to solve my challenge & it works fine. Keeping it simple I use the following:

<form id="newuser" action="newuser.php" onsubmit="return pswderr(this)">

For the button I have

<input id='submit' type="submit" value="Login" onClick="return pswderr();">

My script is:

<script>
function pswderr() {
    var pswd1 = document.getElementById("newuserpswd").value;
    var pswd2 = document.getElementById("rptpswd").value;
    if (pswd1 !== pswd2) {
        document.getElementById("alarm").innerHTML = "Password and password  
        verification do not match. Retry";
        return false;
    } else {document.getElementById("alarm").innerHTML = "";
        return true;
        }
}
</script>
like image 25
Olu Adabonyan Avatar answered Sep 27 '22 18:09

Olu Adabonyan