I am working on a form where I want to add some validation parameters. It is a text field and I want when the user types it validates against a regex. I am using an OnInput event. When it fails, the validation parameters a label should be shown when it succeeds the validation parameter is hidden. This doesn't seem to work,,
Please assist?
Markup:
<input type="text" name="fName" id="fName" placeholder="First Name ..." class="input" required>
<label for="fName" id="firstNameErr" style="color: red; font-size: 14px; display: none;">Invalid First Name </label>
jQuery OnInput validation:
$('#fName').on("input" , function(e) {
var value = $('#fName').val();
//Regex
var fReg = /^[a-zA-Z'` ]+$/i
//If true hide error label
if (fReg.test(value)) {
$("label#firstNameErr").hide();
}
else{
$('label#firstNameErr').show();
}
});
<input>exist when that code runs? Any errors in dev tools console?