I've been struggling to validate a Saudi Arabia phone number on my Bootstrap form, but I keep running into an invalid error.
Here is the current regex pattern I am using:
/^(009665|9665|\+9665|05|5)(5|0|3|6|4|9|1|8|7)([0-9]{7})$/
function checkPhoneNumber(input) {
let phoneNumber = input.val();
let pattern = /^(009665|9665|\+9665|05|5)(5|0|3|6|4|9|1|8|7)([0-9]{7})$/;
if (pattern.test(phoneNumber)) {
$(input).addClass('is-valid');
} else {
$(input).addClass('is-invalid');
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" ></script>
<form class="needs-validation" method="POST" action="" onsubmit="checkPhoneNumber()">
<div>
<label for="contact_no1">Contact Number</label>
<input id="contact_no1" name="contact_no1" type="tel" class="form-control" id="contact_no1" placeholder="" value="" required>
<div class="invalid-feedback">
Please enter a valid contact number.
</div>
</div>
</form>