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
LVL 2
BILL CarlisleAPEX DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SSupremeCommented:
BILL CarlisleAPEX DeveloperAuthor Commented:
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?
SSupremeCommented:
Try now: https://regex101.com/r/eA3bF9/4
click on the left "Unit tests" and play
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

BILL CarlisleAPEX DeveloperAuthor Commented:
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 CarlisleAPEX DeveloperAuthor Commented:
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 ?
SSupremeCommented:
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.
BILL CarlisleAPEX DeveloperAuthor Commented:
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 CarlisleAPEX DeveloperAuthor Commented:
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!
SSupremeCommented:
Replace regex as needed
var re = /^(?:\s*(?:x\.?|ext\.?)\s*(\d{1,10}))?$/gi;
    var str = 'X 458534';
    var m;
     
    if ((m = re.exec(str)) !== null) {
    if (m.index === re.lastIndex) {
    re.lastIndex++;
    }
    // View your result using the m-variable.
    // eg m[0] etc.
    }

Open in new window

From generator

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BILL CarlisleAPEX DeveloperAuthor Commented:
Awesome! thanks for the help!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.