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

Hi @Robbie , 

 

we are very close now - when we can allow that we can enter Zero after the first digit and length can up to 30 digits in total, then we got it. 

 

Thank you

Hi @Robbie , 

@Amit Verma 

 

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

Thank you all

aryanjain25
Giga Guru

Hi @BuriB , you can try the following regex: 

/^[1-9]\d{2}(-)?[1-9]\d{2}(-)?[1-9]\d{2}$/

Thanks,

Aryan Jain

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