I have a text input with a custom oninvalid message:
<input id="foo" type="text" maxlength="16" required pattern="[a-zA-Z0-9]+"
oninvalid="this.setCustomValidity('Please enter letters and /
or numbers only.')" onBlur="chk()" autofocus>
In the onBlur event I want to run some code, but only if the text entered passed the pattern validation specified. Something like:
function chk() {
if (document.getElementById("foo").isvalid?()) {
bar();
}
}
Alternatively:
function chk() {
if ($("#foo").isvalid?()) {
bar();
}
}
Update
I found the following solution, but it does repeat the code of the pattern validation:
function chk() {
var exp = /^[a-zA-Z0-9]+$/;
if (exp.test($("#foo").val())) {
bar();
}
}