Link to home
Start Free TrialLog in
Avatar of Mark B
Mark BFlag for United States of America

asked on

Reg Ex to check for an "X" or "x" in Phone Extension Value- first value especially, but anywhere in the value

I need to add validation to a text input on a web app form to prevent a user from entering a "x" character in a phone extension text field. The web app form allows to use RegEx validation.

I've tried a few different expressions like /^((?!x).)*$/gi and what I'm finding, is that if I enter "x12345", the expression will let me know an "x" was entered, however, if I only remove the "x" and validate again, the site still thinks there is an "x" in the value. If I completely remove all characters, it will then validate there is not an "x".

To help understand if there is an issue with the web app, what is the 'best' example to use for testing? I need to make sure the validation checks for an upper or lower case x in any part, but especially the very first character as that is how users will enter an x. They will either enter "x 12345" or "x12345" or X 12345" or "X12345" (the number of numbers could be any number of numbers though).
Avatar of wilcoxon
wilcoxon
Flag of United States of America image

The simplest regex to check for an X would be: /x/i

If you need to check for not an X, wouldn't it be easiest to just make sure the extension is numeric?  If so, then /^\d+$/ is what you want.
Avatar of Dr. Klahn
Dr. Klahn

There are so many different flavors of regex formats that an off-the-cuff answer could well be wrong.  What format does the application use?  Javascript, PCRE, Python, basic, extended?  Do you need an affirmative match ("yes, there is an x in this" or a negative match ("no, there is no x in this")?  The affirmative match is easier to do.

In this case rather than dragging in the regex library which is a fair amount of overhead, one might instead simply lowercase the input and scan it for "x".  Easier and does not require a regex.

Side note:  Web site regex validators should be approached with some caution.  Reload the page and clear your browser's cache before attempting each new regex test.  Previous inputs can remain in the fields even though unseen.
ASKER CERTIFIED SOLUTION
Avatar of Terry Woods
Terry Woods
Flag of New Zealand 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
Avatar of Mark B

ASKER

Hello, I ended up looking at something setup on a different project and ending up using this: ^((?![xX]).)*$.
Why did you award points only to the last answer you received?  You should have split the points across the answers.
Avatar of Mark B

ASKER

Wilcoxon- should I have marked all three as the solution to split points?
I believe so.  I actually haven't asked a question on here since the interface change (which was a while ago now).