0

on the simple website I am building, I am requesting users phone number which is meant to come in either

080-0000-0000 COULD BE 090-0000-0000 OR EVEN 070-0000-0000

OR

080-0000-00000 COULD BE 090-0000-00000 OR EVEN 070-0000-00000

How do I use Javascript Regex test to check pattern, I can do it PHP but Javascript is giving issues this is what I tried.

var x_checkout_phone_number = "080-0000-0000" OR COULD BE "080-0000-00000" OR THE USER COULD SEND SOMETHING WRONG EG "080-00000000" 
var phoneRegex = new RegExp(/\d{3}-\d{4}-\d{4}/);
var phoneRegex_1 = new RegExp(/\d{3}-\d{4}-\d{5}/);
var phone_valid = phoneRegex.test(x_checkout_phone_number);
var phone_valid_d = phoneRegex_1.test(x_checkout_phone_number);
if (!phone_valid || !phone_valid_d) {
alert ("Invalid phone number format")
return false;   
} else {
alert ("Valid phone number format")
}

How do I validate the input coming in. Thank you.

Shasha
  • 439
  • 8
  • 26

3 Answers3

2

Just combine your regular expressions into one:

/0[7-9]0-(\d{3}-\d{3}|\d{4}-\d{5})/

sorry, I don't do it carefully. it should be

/0[7-9]0-\d{4}-(\d{4}|\d{5})/

for a variable regular expression context

var rgxStr = "0[1-9]\\d-\\d{4}-(\\d{4}|\\d{5})";
var rgxPhone = RegExp(rgxStr + some_thing_else);

for fix regular expression context

var rgxPhone = /0[1-9]\d-\d{4}-(\d{4}|\d{5})/;

or

var rgxPhone = RegExp("0[1-9]\\d-\\d{4}-(\\d{4}|\\d{5})");
Jack Ting
  • 552
  • 4
  • 6
  • Almost there, but I edited the regex code you gave me a little to this var phoneRegex = new RegExp(/0[1-2-3-4-5-6-7-8-9][1-2-3-4-5-6-7-8-9]-\d{4}-(\d{4}|\d{5})/); - this runs, but When I add zero to the last bracket like this var phoneRegex = new RegExp(/0[1-2-3-4-5-6-7-8-9][0-1-2-3-4-5-6-7-8-9]-\d{4}-(\d{4}|\d{5})/); - this does not run – Shasha Apr 28 '20 at 21:28
  • No, if you want miss under stand the javascript `RegExp()`. If the regular expression context is fix, just use `var phoneRegex = /0[1-9]\d-\d{4}-(\d{4}|\d{5})/`. or the context will vary then use `var phoneRegex = new RegExp( string )` and you should translate the context to a string, that is `"0[1-9]\\d-\\d{4}-(\\d{4}|\\d{5})"` – Jack Ting Apr 28 '20 at 21:41
  • Ok, thanks, I played and fixed it, check below, thanks again, really appreciate. – Shasha Apr 28 '20 at 21:46
  • Why `(\d{4}|\d{5})` rather than `(\d{4,5})`? – Cary Swoveland Apr 28 '20 at 22:01
  • both are OK. Why I use `(\d{4}|\d{5})` is I just modify the old one and not thinking of the better one `\d{4,5}`. – Jack Ting Apr 28 '20 at 22:11
2

Take this regex: @0[789]0-\d{4}-(\d{4}|\d{5})$

You can reach that also in PHP:

<?php
$checked_variable = "090-0000-12345";
if (preg_match("@0[789]0-\d{4}-(\d{4}|\d{5})$", $checked_variable)) {
echo "Invalid phone number format";
}
else
{
echo "Valid phone number format";
}
?>

or in JS:

var str = "090-0000-12345"; 
var res = str.match(/0[789]0-\d{4}-(\d{4}|\d{5})/g);
SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
0

I did it with

var x_checkout_phone_number = "081-2222-2222";
var phoneRegex = new RegExp(/^0[1-2-3-4-5-6-7-8-9][0-9]-\d{4}-(\d{4}|\d{5})$/);
var phone_valid = phoneRegex.test(x_checkout_phone_number);
if (!phone_valid) {
alert ("Invalid phone");
} else {
alert ("Validphone");
}
Shasha
  • 439
  • 8
  • 26