Let’s say i have the following text input:
<input class="required-field" type="text" name="create-user-username" pattern="/^[0-9]+$/" />
Somewhere in my JS i (would like to) use the follow snippet in my form validation:
$('.required-field').each(function() {
var inputVal = $(this).val();
var pattern = $(this).attr('pattern');
if (pattern !== 'undefined') {
if (inputVal.match(pattern)) {
alert('Input is not valid');
}
}
});
However, the match is not working and I have failed in finding a working solution…
The form is completed by the user in steps, and each pages are validated before you can move to next step. That why I’m not using jQuery .validate….
patternis a string, thus is needs to be:also, use either
and fix your mismatching parens: if
(inputVal.match(pattern) // <- missing )