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>
/^937\d{8}$/ornew RegExp("^937\\d{8}$")new RegExp('^\+937\d{8}$');and looks good to me. If 9371-9376 are invalid, thennew RegExp('^937[0789]\d{7}$');should work.