Solved

How to get regular expresson ti limirt number of digits to be entered by user

Posted on 2009-05-04
5
393 Views
Last Modified: 2012-05-06
This would seem very simple because I only want my user to be able to enter up to 2 digits followed by a . char followed by up to 2 decimal places.

So in other words a floating point value such as 99.99 or 3.45 or 37.8 but not 888 or 888.88.

Now my Javascript regular expression to allow up to 2 digits before the floating point looks like this:

var regExp = /[0-9]{1,2}/

but what I don't understand is that this expression returns true when I call
regExp.test("888"); ?????

Can anyone explain this and tell me what I should be putting to ensure the 3rd digit causes an invalid result?

Many thanks
Sam

0
Comment
Question by:Samm1502
5 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 250 total points
Comment Utility
/[0-9]{1,2}/ can match either the first 88 or the last 88 in "888"
/^[0-9]{1,2}\./
would only allow . and not 8 after the first 88
0
 
LVL 9

Expert Comment

by:ghostdog74
Comment Utility
yes its actually quite simple that you don't need a regular expression for that.  so according to this

>> So in other words a floating point value such as 99.99 or 3.45 or 37.8 but not 888 or 888.88.

you can
1) check whether the string has a decimal "." .
2) if there's decimal
     a) split the string on "." as delimiter.
     b) check 1st element and 2nd element whether they less than 99 and more than 0.


0
 

Author Comment

by:Samm1502
Comment Utility
Thanks ghostdog74 but I have to use a regular expressions for reasons I won't bore you with and ozo if I read this correctly it test for the existence of any char not in the range [0-9] but I need to make sure my regexp.test call returns true if the entered value is valid and false otherwise so how do I alter my original expression to prevent it matching part of the entered string and force it match all of it i.e. allwo just 1 or 2 digits before the . and 1 or 2 digits afterwards?

Many thanks.
Sam
0
 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 250 total points
Comment Utility
In ozo's solution
/^[0-9]{1,2}\./
the ^ matches the start of the string.

Altering it to restrict it to 1 or 2 digits after the decimal would be:
/^[0-9]{1,2}\.[0-9]{1,2}$/

$ matches the end of the string.
0
 

Author Closing Comment

by:Samm1502
Comment Utility
Thanks for the explanation. Cheers Sam
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now