Avatar of BILL Carlisle
BILL Carlisle
Flag 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
Regular ExpressionsJavaScriptjQuery

Avatar of undefined
Last Comment
BILL Carlisle

8/22/2022 - Mon
SSupreme

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?
SSupreme

Try now: https://regex101.com/r/eA3bF9/4
click on the left "Unit tests" and play
Your help has saved me hundreds of hours of internet surfing.
fblack61
BILL Carlisle

ASKER
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,}
BILL Carlisle

ASKER
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 ?
SSupreme

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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
BILL Carlisle

ASKER
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');
}
BILL Carlisle

ASKER
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
SSupreme

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
BILL Carlisle

ASKER
Awesome! thanks for the help!
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23