stopping form submit if the validation fails.

后端 未结 7 2279
青春惊慌失措
青春惊慌失措 2020-12-05 21:17

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 subm

7条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-05 22:10

    Just a comment:

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

    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;
    }
    

提交回复
热议问题