Możesz to zaimplementować samodzielnie, korzystając z setCustomValidity()
. W ten sposób, this.checkValidity()
odpowie na dowolną regułę, którą chcesz zastosować do swojego elementu. Nie sądzę, aby this.validity.patternMismatch
można było ustawić ręcznie, ale w razie potrzeby można użyć własnej właściwości.
http://jsfiddle.net/yanndinendal/jbtRU/22/
$('#test').keyup(validateTextarea);
function validateTextarea() {
var errorMsg = "Please match the format requested.";
var textarea = this;
var pattern = new RegExp('^' + $(textarea).attr('pattern') + '$');
// check each line of text
$.each($(this).val().split("\n"), function() {
// check if the line matches the pattern
var hasError = !this.match(pattern);
if (typeof textarea.setCustomValidity === 'function') {
textarea.setCustomValidity(hasError ? errorMsg : '');
} else {
// Not supported by the browser, fallback to manual error display...
$(textarea).toggleClass('error', !!hasError);
$(textarea).toggleClass('ok', !hasError);
if (hasError) {
$(textarea).attr('title', errorMsg);
} else {
$(textarea).removeAttr('title');
}
}
return !hasError;
});
}
Wydaje [ 'textarea' nie mamy atrybut pattern''] (https: // developer.mozilla.org/en-US/docs/HTML/HTML_Elements/textarea?redirectlocale=en-US&redirectslug=HTML%2FElement%2Ftextarea), więc prawdopodobnie przeglądarki go zignorują. – Passerby