After some research, I've came up with the following code that should be the answer to your question. (At least it worked for me)
Use this piece of code first. The $(document).ready
makes sure the code is executed when the form is loaded into the DOM:
$(document).ready(function()
{
$('#theIdOfMyForm').submit(function(event){
if(!this.checkValidity())
{
event.preventDefault();
}
});
});
Then just call $('#theIdOfMyForm').submit();
in your code.
UPDATE
If you actually want to show which field the user had wrong in the form then add the following code after event.preventDefault();
$('#theIdOfMyForm :input:visible[required="required"]').each(function()
{
if(!this.validity.valid)
{
$(this).focus();
// break
return false;
}
});
It will give focus on the first invalid input.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…