1

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.

Here are the default phone numbers used in Afghanistan:

+93785657024
+93795657024
+93700565656
+93775657024

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.

Here is the Javascript code I am trying to fix. This code now works only for +937 followed by 8 digits.

<script type="text/javascript">
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");
  }
}
</script>
dda
  • 6,030
  • 2
  • 25
  • 34
Nargis Akbari
  • 15
  • 1
  • 5
  • 2
    http://stackoverflow.com/q/123559 – Robert Harvey Jul 01 '13 at 17:44
  • Either `/^937\d{8}$/` **or** `new RegExp("^937\\d{8}$")` – Bergi Jul 01 '13 at 17:45
  • @Bergi That would match stuff without the plus sign and combinations that are not ok like 9370, 9371, 9372, 9373, 9374, 9375, 9376... – dda Jul 01 '13 at 17:53
  • you should consider input type tel combined with pattern attribute instead of JS validator – Jan Turoň Jul 01 '13 at 18:37
  • @dda, 9370 is invalid? Number is present above, isn't it? And for the plus, just add it in: `new RegExp('^\+937\d{8}$');` and looks good to me. If 9371-9376 are invalid, then `new RegExp('^937[0789]\d{7}$');` should work. – vapcguy Aug 14 '14 at 02:59

4 Answers4

2

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.


This answer will not work except in the 700 case because the OP specified that all prefixes were followed by 6 digits, when in fact only 93700 is followed by 6, and the other prefixes are followed by 7 digits.

/^\+93(77\d|78\d|79\d|700)\d{6}$/

should do it.

Your original regex doesn't require a "+" at the front though.

Community
  • 1
  • 1
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
  • I tried this but it is not working. I also tested this with [link](http://www.regular-expressions.info/javascriptexample.html) and says no match. – Nargis Akbari Jul 01 '13 at 18:11
  • 1
    This answer will not work except in the 700 case because the OP specified that all prefixes were followed by 6 digits, when in fact only 93700 is followed by 6, and the other prefixes are followed by 7 digits. (But the regular expression does provide exactly what the OP asked for!) – Joseph Myers Jul 01 '13 at 18:40
  • I changed '/^\+93(77|78|79|700)\d{6}$/' to '/^\+93(77|78|79|70)\d{}$/' but still it says invalid number. I also removed + sign from it. Now I have this line '93(77|78|79|70)\d{7}' – Nargis Akbari Jul 01 '13 at 18:57
  • @MikeSamuel Looks good to me. (P.S. I never down voted your answer. I never down vote in general, unless something is *horrifically* wrong.) – Joseph Myers Jul 02 '13 at 03:49
1

I think this would suit your condition

Totally you need 11 digits prefixed with +.

Therefore if 77|78|79 means followed by 7 digits or if 700, followed by 6 digits.

var reg = /^\+93((77|78|79)\d{7}|700\d{6})$/;
console.log(reg.test('+93705657024'));

Check this Fiddle

Praveen
  • 55,303
  • 33
  • 133
  • 164
1

Regular expression itself:

var phoneRegex = new RegExp('(937(?:00\\d{6}|[789]\\d{7}))');

Notes:

  • Since you are using new RegExp, I have not inserted / and / 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.)
  • 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.
  • 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 ?: that my regular expression contains.)
  • My expression does not anchor the match to the beginning or end of the string, because doing so would return false for valid numbers that had punctuation, like the + that you showed in your own phone numbers.
  • 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 false even though they are actually valid.
  • This modification of your code would work for ensuring that valid phone numbers are recognized correctly by first removing punctuation:

    if(!(phoneRegex.test(phoneNum.replace(/\D+/g, '')))) {
    
  • If you make this modification to your code, then it would be appropriate to add the anchoring expressions ^ and $ 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.

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:

<script type="text/javascript">
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();
  }
}
</script>
<input id="receiver" value="+93785657024" />
<input type="button" onclick="test()" value="Check this number" />

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.

Joseph Myers
  • 6,434
  • 27
  • 36
  • Thanks for detail explanation. I removed the + sign as you suggested. 'var phoneRegex = new RegExp("93(77|78|79|70)\d{7}");' but still it returns error. – Nargis Akbari Jul 01 '13 at 18:47
  • Try pasting in the entire line of code that I wrote. var phoneRegex = new RegExp('(937(?:00\d{6}|[789]\d{7}))'); (I tested it, too, and it works for me.) – Joseph Myers Jul 01 '13 at 18:49
  • I pasted the entire code and then check with this number 93700600708 so it says invalid phone number. – Nargis Akbari Jul 01 '13 at 18:53
  • There seems to be a problem with your code in the way it evaluates the test result, because my code is returning a match when I debug the match. Your code right now returns invalid even when I change the expression to \d{8} for example (which should obviously return valid.) – Joseph Myers Jul 01 '13 at 18:57
  • I modified your code and now it detects the phone numbers properly. http://dropoff.us/private/1372705203-1-so-answer1.html – Joseph Myers Jul 01 '13 at 19:00
  • I changed the syntax to '[0-9]' and then tested with digits. It worked. So this mean the code is okay? – Nargis Akbari Jul 01 '13 at 19:01
  • @NargisAkbari My answer is now updated to a fully-working version of the code. You can ignore the link that I posted. (I accidentally pasted something wrong into the middle of it and uploaded that version accidentally.) – Joseph Myers Jul 01 '13 at 19:05
  • Yes, that's what I meant when I said that I did not put ^ and $ around the regular expression. You can add those, but then things like + signs will become invalid. I believe that you should add this modification first: "if(!(phoneRegex.test(phoneNum.replace(/\D+/g, '')))) {" Note: I have to go to my university now, so I will not be able to respond for a while. I will update my answer one last time before I go to implement this for you. – Joseph Myers Jul 01 '13 at 19:10
  • It is almost fixed. It says valid even If the followed digits are more than 10. – Nargis Akbari Jul 01 '13 at 19:11
  • The latest version removes punctuation and ensures there are the right number of digits as well. – Joseph Myers Jul 01 '13 at 19:13
  • Thanks for everything. I limited the number of characters in text box and works perfect with your code. ' ' – Nargis Akbari Jul 01 '13 at 19:35
  • @NargisAkbari Thank you. I just got to my office now, and I am happy to hear that it is working for you now! – Joseph Myers Jul 01 '13 at 19:35
0

Use:

/^\+937([789]|00)\d\d\d\d\d\d$/

That should do it.

+937 All your numbers start with this
([789]|00) Either 7, 8, 9 or 00
\d\d\d\d\d 6 digits

dda
  • 6,030
  • 2
  • 25
  • 34