Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Regular expression itself:</p> <pre><code>var phoneRegex = new RegExp('(937(?:00\\d{6}|[789]\\d{7}))'); </code></pre> <p>Notes:</p> <ul> <li>Since you are using <code>new RegExp</code>, I have not inserted <code>/</code> and <code>/</code> around the expression. (I am mentioning this because you commented that some of the other answers did not work at all, and I suspect that is because you may have forgotten to remove the / and / before testing them, perhaps? There seem to be minor problems with them which is why I am also answering, but the other answers should at least match some valid phone numbers if you remove the / and / that surround the regular expressions.)</li> <li>You seem to have made a mistake in counting the digits vs. the examples you gave of valid Afghanistan phone numbers. +9377, +9378, etc. are followed by 7 digits rather than 6, while only +93700 is actually followed by 6.</li> <li>My expression puts the full phone number into the temporary variable $1, which can come in handy. (I used a non-capturing group for the alternatives in the second part of the match because extra captures sometimes can be a bother when I want to do something else with the results of the match. For what your code is doing, it would work the same with or without the <code>?:</code> that my regular expression contains.)</li> <li>My expression does not anchor the match to the beginning or end of the string, because doing so would return <code>false</code> for valid numbers that had punctuation, like the <code>+</code> that you showed in your own phone numbers.</li> <li>For the same reason, I suggest that you delete non-numeric characters before checking input for a valid phone number. Right now phone numbers using parentheses or hyphens or any traditional punctuation will return <code>false</code> even though they are actually valid.</li> <li><p>This modification of your code would work for ensuring that valid phone numbers are recognized correctly by first removing punctuation:</p> <pre><code>if(!(phoneRegex.test(phoneNum.replace(/\D+/g, '')))) { </code></pre></li> <li><p>If you make this modification to your code, then it would be appropriate to add the anchoring expressions <code>^</code> and <code>$</code> around my suggested regular expression to make sure it only contained a valid phone number, not a longer number such as a credit card number.</p></li> </ul> <p>There is a problem with the way your code checks the results from doing the regular expression test. This code will work better for you:</p> <pre><code>&lt;script type="text/javascript"&gt; function test (){ var phoneField = document.getElementById("receiver") var phoneNum = phoneField.value; if (/^(937(?:00\d{6}|[789]\d{7}))$/.test(phoneNum.replace(/\D+/g, ''))) { alert('valid'); } else { alert('Invalid phone number'); phoneField.focus(); } } &lt;/script&gt; &lt;input id="receiver" value="+93785657024" /&gt; &lt;input type="button" onclick="test()" value="Check this number" /&gt; </code></pre> <p>Note: I got some of my code mixed up. Please ignore my other code (the one that was uploaded to that link, I mean). This one works.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload