3
I’m trying to get you to add a Class
is an element through its ID if it is a certain kind of pattern.
if (/^([0-9]{4}[\-]?[0-9]{3})$/.test('#codigoPost')) {
$('#codigoPost').removeClass("is-invalid");
$('#codigoPost').addClass("is-valid");
} else {
$('#codigoPost').removeClass("is-valid");
$('#codigoPost').addClass("is-invalid");
}
.is-valid(
border: 1px solid red;
)
.is-invalid(
border: 1px solid green;
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
<div>
<label class="label-noPadding" for="codigoPost">Código Postal</label>
<input type="number" class="form-control" name="codigoPost" id="codigoPost" pattern="[0-9]{4}[\-]?[0-9]{3}" placeholder="0000-000" required="true">
</div>
</form>
Basically, if it is of the genus "1234-123", "2222-333" or "9876-543". 4 numbers, followed by a "-" and followed by 3 numbers add to class
.
But I don’t see what I’m doing wrong here, he always gives false
entering the else
. I would appreciate your help.
puts the rest of the html too if it is not impossible to test your snippet because there is no element with this #id
– Lodi
@Lodi So it gets better?
– Pbras