Javascript validation validation

I get a regular expression that checks the percentage of 0 to 100 and allows two decimal places.

Does anyone know how to do this or know a good website that has an example of regular regular expressions used for client-side validation in javascript?

@Tom - Thanks for the questions. Ideally, there would be no leading 0 or other end characters.

Thanks to everyone who answered so far. I found the comments really interesting.

+7
javascript regex
source share
7 answers

Instead of using regular expressions to do this, I would simply convert the user-entered number to a floating point value, and then check the range you want (from 0 to 100). Trying to validate a number range with regular expressions is almost always the wrong tool for the job.

var x = parseFloat(str); if (isNaN(x) || x < 0 || x > 100) { // value is out of range } 
+27
source share

I suggest the following:

 (^100(\.0{1,2})?$)|(^([1-9]([0-9])?|0)(\.[0-9]{1,2})?$) 

It corresponds to 100, 100.0 and 100.00 using this part

 ^100(\.0{1,2})?$ 

and numbers such as 0, 15, 99, 3.1, 21.67, using

 ^([1-9]([0-9])?|0)(\.[0-9]{1,2})?$ 

Pay attention to which leading zeros are forbidden, but trailing zeros are allowed (although no more than two decimal places).

+9
source share

It reminds me of an old blog post by Alex Papadimoulis ( The Daily WTF ), where he tells the following story:

"The client asked me to create and install a special shelving system. I am at the point where I need to nail it, but I'm not sure what to use to hammer it. Should I use an old boot or a glass bottle?"

How would you answer the question?

  • It depends. If you are looking for a pound of a small (20 pounds) nail in something like drywall, it will be easier for you to use the bottle, especially if the shoes are dirty. However, if you are trying to drive a heavy nail into a forest, go with a boot: a bottle with a break in your hand.

  • There is something fundamentally wrong with how you build; you need to use real tools. Yes, this may be due to a trip to the toolbar (or even to a household appliance store), but at the same time, the right way will save a lot of time, money and aggravate the life cycle of your product. You need to stop building things for money until you understand the basics of construction.

This is such a question when most people see it as a challenge to come up with the right regular expression to solve the problem, but it would be much better to say that using regular expressions uses the wrong tool for the job.

The problem with trying to use a regular expression to test numerical ranges is that it is difficult to change if the requirements for the allowed range change. Today, the requirement may be to check numbers from 0 to 100, and you can write a regular expression for what does not make your eyes bleed. But next week, a change may be required, so values ​​from 0 to 315 are acceptable. Good luck changing your regular expression.

The solution given by Greg Hugill is probably better - even if it confirms "99fxx" as "99". But given the circumstances, which may be in order.

+5
source share

Given that your value is in str

 str.match(/^(100(\.0{1,2})?|([0-9]?[0-9](\.[0-9]{1,2})))$/) 
+1
source share

(100|[0-9]{1,2})(\.[0-9]{1,2})?

This should be the regular expression you want. I suggest you read Mastering Regular Expression and download RegexBuddy or Regex Coach.

0
source share
 ^100(\.(0){0,2})?$|^([1-9]?[0-9])(\.(\d{0,2}))?\%$ 

This will match:
100.00
optionally "1-9" followed by a digit (this makes the inside), optionally followed by a dot and two digits


From what I see, Greg Huglill’s example doesn’t actually work that well, because parseFloat ('15x') will simply return 15, which will meet the condition 0 <x <100. Using parseFloat is clearly wrong, as it does not check the percentage value, it tries to force the check, Some people here complain about leading zeros, and some ignore trailing invalid characters. Maybe the author of the question should edit it and make it clear what he needs.

0
source share

@mlarsen: Isn't it that the regex here won't improve the job.

Remember that validation will be performed both on the client side and on the server side, so that:

 100|(([1-9][0-9])|[0-9])(\.(([0-9][1-9])|[1-9]))? 

will be validated in different languages, just beware of checking input length with output matching length.

0
source share

All Articles