title: Javascript regex to validate a phone number
body: <p>I am working to modify a script. The script contains a form to allow visitors to send an SMS to phone numbers. In the form I have a text box in which users enter the phone number of the text receiver. I am using a regex to validate the phone number to prevent spammers and make sure the user typed the correct number.</p>
  
  <p>Here are the default phone numbers used in Afghanistan:</p>
  
  <pre><code>+93785657024
  +93795657024
  +93700565656
  +93775657024
  </code></pre>
  
  <p>The regex validation first should make sure that +93 is used, then make sure that 78, 77, 79 or 700 (one of these) is used after +93 and finally followed by 6 digits.</p>
  
  <p>Here is the Javascript code I am trying to fix. This code now works only for +937 followed by 8 digits.</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  function test (){
    var phoneRegex = new RegExp('^937\d{8}$');
    var phoneNum = document.getElementById("receiver").value;
    if(!(phoneRegex.test(phoneNum))) {
      alert("Invalid Phone Number");
      document.getElementById("receiver").focus();
    } else {
      alert("valid");
    }
  }
  &lt;/script&gt;
  </code></pre>