Regex for Digit and Dashes (Phone number)

BuriB
Tera Expert

Hi All, 

we will restrict phone regex 

We will allow digits with dashes in between, but the dashes could be optional. 

Example: 555555555 or 555-555-555 (no leading zero)

Currently we have the following regex: 

 

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue == '') {

        return;

    }

    var regex = /^[1-9][0-9]{0,29}$/; // regex for phone numbers

 

How can we solve this? Could you please help 

 

2 ACCEPTED SOLUTIONS

Robbie
Kilo Patron
Kilo Patron

Hi @BuriB,

 

I've tweaked it slightly and tested it to ensure it works as required:

(Please note, the 4 highlighted in bold in the ending brackets it the number used to determine the minimum and maximum number. So in using the regex as shown below you can have 9 minimum (3+3+3) and 10 maximum (3+3+4). If you want to extending this to 11 numbers for example, change the 4 to a 5.)

 

/^[1-9]{3}(-)?\d{3}(-)?\d{3,4}$/gm;

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.



Thanks, Robbie

View solution in original post

Hi @Robbie , 

@Amit Verma 

 

I got it = /^[1-9](-)?[0-9 \-]{0,29}$/;

Thank you all

View solution in original post

8 REPLIES 8

Robbie
Kilo Patron
Kilo Patron

Hi @BuriB,

 

(Updated since original post @BuriB )

The regex required to match either '123123123' or '123-123-123' with no leading zero and no spaces is:

 

/^\d{3}(-)?\d{3}(-)?\d{3}$

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.



Thanks, Robbie

@Robbie 

 

hi Robbie, 

 

its not working as expected. It allows leading zero and is restricted to 9 digits in total but the number could more than 9 digits

Hi @BuriB 

 

Please try with below regex :

 

/^[1-9 \-]+$/gm

 

 

Output :

 

AmitVerma_0-1726825037951.png

 

AmitVerma_1-1726825053465.png

 

AmitVerma_2-1726825069482.png

 

AmitVerma_3-1726825081175.png

 

Thanks and Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

Hi @BuriB,

 

I've tweaked it slightly and tested it to ensure it works as required:

(Please note, the 4 highlighted in bold in the ending brackets it the number used to determine the minimum and maximum number. So in using the regex as shown below you can have 9 minimum (3+3+3) and 10 maximum (3+3+4). If you want to extending this to 11 numbers for example, change the 4 to a 5.)

 

/^[1-9]{3}(-)?\d{3}(-)?\d{3,4}$/gm;

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.



Thanks, Robbie