Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saudi Arabia mobile and land line numbers regex validation

I have been trying to validate Saudi Arabia phone number on my bootstrap form, but I keep getting invalid error.

Here is the regex I am using presently.

/^(009665|9665|\+9665|05|5)(5|0|3|6|4|9|1|8|7)([0-9]{7})$/

function validateContact(thisObj) {
  let fieldValue = thisObj.val();
  let pattern = /^(009665|9665|\+9665|05|5)(5|0|3|6|4|9|1|8|7)([0-9]{7})$/;
  if (pattern.test(fieldValue)) {
    $(thisObj).addClass('is-valid');
  } else {
    $(thisObj).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="validate()">
  <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>
like image 919
Olasunkanmi Avatar asked Dec 07 '25 11:12

Olasunkanmi


1 Answers

This pattern is easy to match the numbers:

^((?:[+?0?0?966]+)(?:\s?\d{2})(?:\s?\d{7}))$
like image 162
Amrendra Kumar Avatar answered Dec 09 '25 23:12

Amrendra Kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!