I'm trying to use the HTML 5 validation for user login. However, after the form is submit I use ajax to authenticate on the server. If that fails I use the setCustomValidity(msg)
method. However the method does not draw/show the validation message, but rather shows it on the next submit or change. How can I force it to draw immediately?
Additionally, there is a maxlength
constraint but is there a minlength
?
Below is the HTML form and javascript. At the moment after submit if the form is invalid the message get's set but is not drawn.
$(function() {
$('form#login_form').submit(function(e) {
console.log("submit");
$.post('/auth_user', $(this).serialize(), function (data) {
console.log(data);
if(data != "") {
login_password.setCustomValidity(data);
}
});
e.preventDefault();
});
});
<form action="/dashboard" id="login_form" method="post">
<div class="field-wrap">
<label>
Email Address<span class="req">*</span>
</label>
<input type="email" name="email" id="login_email" required autocomplete="off"/>
</div>
<div class="field-wrap">
<label>
Password<span class="req">*</span>
</label>
<input type="password" name="password" id="login_password" required autocomplete="off"/>
</div>
<p class="forgot"><a href="#">Forgot Password?</a></p>
<button type="submit" class="btn btn-lg btn-primary"/>Log In</button>
</form>