Need 2 different Regex functions

I need 2 different regex functions to apply to 2 different fields in a web form that I'm developing:

1) Phone number - need regex that only allows numbers, parentheses and hyphens, etc.
2) Quantity - need regex that requires that the value be numeric, and the number value should be at least 25 or greater.

Please advise.

Thanks!
- Yvan
egoselfaxisAsked:
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.

ozoCommented:
regex that only allows numbers, parentheses and hyphens
^[\d()\-]*$

regex that requires that the value be numeric, and the number value should be at least 25
2[5-9]|[3-9]\d|[1-9]\d{2,}

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
RobOwner (Aidellio)Commented:
While the regex might work for you, you should also take a look at the validation plugins that are out there for phone numbers and restricting fields based on criteria such as only allowing numbers.

This is the standard for working with validating web forms (uses jQuery) http://jqueryvalidation.org/

As for the number issue, all you have to do is try to cast the value as a Number (on the keydown event) and check for isNaN:

<input type="text" value="" onkeydown="checkNumber(event)" />

function checkNumber(e) {
  if (isNaN(String.fromCharCode(e.keyCode))) {
    e.preventDefault();
  }
}

Open in new window


Just remember that any "validation" you do on the client must be repeated on the server as it is easy to bypass for someone that wants to (including any regex you use).
egoselfaxisAuthor Commented:
Thanks Ozo -- those worked great for me!  I just updated the phone number Regex so that it also allows for spaces:

<form method="post" action="">
Phone Number: <input required="required" type="text" name="phone" pattern="^[\d()\- ]*$" title="You must enter a valid Phone Number">
<br /><br />
Minimum Quantity: <input required="required" type="text" name="quantity" pattern="2[5-9]|[3-9]\d|[1-9]\d{2,}" title="You must specify a quantity of 25 or greater"><br /><br />
<input type="submit">
</form>

Open in new window

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.