egoselfaxis
asked on
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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>
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:
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).