Masked Input Plugin and regular expressions (for decimal numbers)

JustTrying picture JustTrying · Aug 1, 2013 · Viewed 11k times · Source

I have a text input field in which I want to insert just numbers, then a comma, and then just two decimal digits.

I'm trying to obtain this via jQuery, using Masked Input Plugin, but it seems not to work.

This is what I have:

<input type="text" name="cash" id="number"/>

and

<script type="text/javascript">
$.mask.definitions['p']='[0-9]+';
$.mask.definitions['d']='[0-9]{2}';
$("#number").mask("p,d");
<script>

But the result is a mask with just one digit, then a comma, and then something I'm not able to write (it's not a digit, neither a character)

_,_

How can I use regular expression with this plugin?

Answer

Tiago C&#233;sar Oliveira picture Tiago César Oliveira · Sep 27, 2013

I'd ratter recommend you to use jquery.maskMoney - https://github.com/plentz/jquery-maskmoney

It's pretty easy to implement:

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

<script src="jquery.maskMoney.js"></script>
<script>
    $("#cash").maskMoney("mask");
</script>