Skip to content Skip to sidebar Skip to footer

Live Control A Float Input With A Regex Mask

I've made a function to live control numbers and float numbers. But it doesn't work properly for float numbers, which have to be like this expression: // I wish a number like x fig

Solution 1:

This regex

(/(^\d+$)|(^\d+.\d+$)|[,.]/)

should match

  • 1111111 per the (^\d+$)
  • or 111111.11111 per the (^\d+.\d+$)
  • or the comma followed by any character, and it could be anywhere in the expression.

I'm suspecting your regex should be Note that I've escaped the final period. That would match a comma or a period

/(^\d+[,\.]{0,1}\d{3})/

may be exactly what you want based on clarifications in the comments

[-+]?[0-9]*\.?[0-9]+

would also work

NOTE: You can simplify your regex life tremendously by using Roy Osherove's Regulazy or the tool Regex Buddy.


Solution 2:

You want arbitrary amount of digits behind the decimal point (comma or period)?

What's wrong with:

/^([1-9][0-9]*|0)([\.,][0-9]+)?$/

I switched out the {3} for + and \. for [\.,]


Post a Comment for "Live Control A Float Input With A Regex Mask"