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.

3
  • try with this solution, stackoverflow.com/questions/6960596/… Commented Apr 28, 2020 at 20:01
  • Instead of two regexes, you can use just one that ends with \d{4,5}. Beside that, you should probably add ^ at the start and $ at the end. Commented Apr 28, 2020 at 20:03
  • Is that actually your JS?! Commented Apr 28, 2020 at 20:10

3 Answers 3

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})");
Sign up to request clarification or add additional context in comments.

5 Comments

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
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})"
Ok, thanks, I played and fixed it, check below, thanks again, really appreciate.
Why (\d{4}|\d{5}) rather than (\d{4,5})?
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}.
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);

Comments

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");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.