Link to home
Start Free TrialLog in
Avatar of BILL Carlisle
BILL CarlisleFlag for United States of America

asked on

javascript/jQuery regular expressions?

Hi,
How do I validate a field that the user enters a phone extension?

they want to allow
' ext. 12345 '
'ext 123456789'
' Ext. 1234567 '
'Ext 1234'
'x123456'

any number of digits say up to 10

I was wanting to use regEx like my other ones but I searched and found those but really do not know what they do??

Thank you,
Bill
Avatar of SSupreme
SSupreme
Flag of Belarus image

Avatar of BILL Carlisle

ASKER

that is a cool tool!

It should ONLY allow those combination types..

xf1234 sent back a true.. which is no good.. ?? x1234 good, xD44444 is no good.

Is it an easy change to tweak it to only allow that list?
Try now: https://regex101.com/r/eA3bF9/4
click on the left "Unit tests" and play
The goal is to "Match"

I will remove leading whitespace then just below (except that 12345.. means [0-9]{1,10}
they want to allow
' ext. 12345 '
'ext 123456789'
' Ext. 1234567 '
'Ext 1234'
'x123456'

I thought this would do it..
(x{1}|ext{1}|ext\.{1}|Ext{1}|Ext\.{1})\s*[0-9]{1,}
Here, I tweaked it because you gave me this cool tool!

(?:\s*(?:x|ext\.?)\s*(\d{1,10})+)?

But now I want to exclude this one
ext.    5t64564

I want the digits to be the last part of the input.. no extra chars

I only want it to match those patterns. only is a + sign ?
It already does what you want, not sure what you are doing.
just add ^ at start and $ at the end. Like this.
^(?:\s*(?:x|ext\.?)\s*(\d{1,10})+)?$
Use "Unit tests" to create tests and test with real values, my list in "Test string" doesn't make any sense as there are many stringS, it just for personal preview that it works.
Why doesn't the tool match any with that?
^(?:\s*(?:x|ext\.?)\s*(\d{1,10})+)?$

I tried
^(x|ext\.?\s)?[0-9]{3,10}$ in the tool but doesn't match anything.

tried this in the console and worked great.. (except for matching Ext and Ext.)
var myExp = /^(x|ext\.?\s)?[0-9]{3,10}$/;
if (myExp.test("ext. 123")){
    console.log('true');
}else{
    console.log('false');
}
Use "Unit tests" to create tests and test with real values, my list in "Test string" doesn't make any sense as there are many stringS, it just for personal preview that it works.

Open in new window


ahh didn't read this.. looks good! thanks!
ASKER CERTIFIED SOLUTION
Avatar of SSupreme
SSupreme
Flag of Belarus image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Awesome! thanks for the help!