Limit text box input to one decimal place

I am new to jQuery. I am just trying to add only one decimal value to a text box using Regex. As it allows ".". But I need to limit myself to just one ".". Can anyone suggest me to do this? Any help would be greatly appreciated.

My code is:

$("#cashReceived").filter_input({regex:'[0-9-.]'});

Thank.

+3
source share
2 answers

You can use this regex to allow only the maximum, but optional decimal point:

$("#cashReceived").filter_input({regex:'[-+]?[0-9]+(\\.[0-9]+)?'});
+1
source

If you use this plugin , you cannot. You better find another library / plugin.

: ( ).

:

...
if (event.type=='keypress') {

  var key = event.charCode ? event.charCode : event.keyCode ? event.keyCode : 0;

  ...

  var string = String.fromCharCode(key);
  ...

if (regex.test(string)) {
+1

All Articles