I have this code which was working on a single class perfectly. I changed this to each class because I added multiple-element with the same class and want to set the same validation error on both fields.
html
<div class="col-xl-6 col-lg-6 col-md-6 col-sm-12 col-12">
<label>Serial number start with 1 or 2</label>
<span>
<input type="text" name="serienummer" value="" maxlength="12" minlength="12" class="serialnumber"
placeholder="00-000000-00">
</span>
<span class="errmsgserienum"></span>
</div>
<div class="col-xl-6 col-lg-6 col-md-6 col-sm-12 col-12">
<label>Serial number start with 1 or 2</label>
<span>
<input type="text" name="serienummer1" value="" maxlength="12" minlength="12" class="serialnumber"
placeholder="00-000000-00">
</span>
<span class="errmsgserienum"></span>
</div>
JS
jQuery(function ($) {
$('.serialnumber').keyup(function (ev) {
$('.serialnumber').each(function (ev) {
var x = $(this).val();
// Allow Backspace and Delete
if (ev.keyCode == 8 || ev.keyCode == 46) {
return true;
}
if (!x.match(/^1|2+/)) {
$(".errmsgserienum").html("Serial number should start with 1 or 2");
$(this).val(x.substr(0, -1));
} else if (x.match(/^1|2+/) && x.length == 12) {
$(".errmsgserienum").html("");
}
});
});
});
After adding each class it is working on all elements at the same time. How can this be work on each class element separately?