简体   繁体   中英

jQuery mask percent for any value

I want to get a mask for a percentage value in JavaScript from 00,00 to 99,99 .

This mask need to accept any value, this is the values that can accept for example

1 11 11,1 11,11

This is my code: HTML:

<input id="per" type="text"></input>

and JS:

$("#per").mask("9?9,99");

But when I try insert 11 this change to 11, . How i remove the , in this cases?

Here is my mask: http://jsfiddle.net/SEXAj/2763/

You can use this with blur event:

$("#per").mask("9?9,99").blur(function(){
  var rg = /\,+\d/g; // regular expression for comma and number
  if(!rg.test(this.value)){ // check if value doesn't have any num after comma
    this.value = this.value.split(',')[0]; // then just split the value and
  }                                        // assign the index[0]
});

Updated Fiddle.

This different mask plugin https://igorescobar.github.io/jQuery-Mask-Plugin/ does exactly what you want. You can try it, follow the link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM