I\'m trying to implement html5 into a form, but I came with a problem when I submit the form through jquery and try to use the html5 \"required\" attribute.
Here is
You can
click
event on a submit$("form")[0].checkValidity()
$("form :invalid")
Here are some examples, how to validate html5:
function validateForm(form) {
if (form.username.value=='') {
alert('Username missing');
return false;
}
return true;
}
<form action="index.php" name="loginform" method="post" onsubmit="if (!validateForm(this)) event.preventDefault();">
<input name="username" placeholder="username" required="required" type="text" />
<a href="" onclick="event.preventDefault(); $(document.forms.loginform).submit();">Login1</a>
<a style="cursor:pointer;" onclick="$(document.forms.loginform).submit();">Login2</a>
<input name="send" type="submit" value="Login3" />
<input name="send" type="button" value="Login4" onclick="$(document.forms.loginform).submit();" />
</form>
Full code here. One thing here is strange: $(document.forms.loginform).submit(); works fine, but document.forms.loginform.submit(); fails. Can somebody explain why?
Working demo http://jsfiddle.net/3gFmC/
Hope this will help :)
code
$('#btn_submit').bind('click', function() {
$('input:text[required]').parent().show();
// do whatever;
});