Question

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?

Was it helpful?

Solution

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>

OTHER TIPS

Try this,

$("#number").mask("99,99");

Working Demo

A quite good plugin for masking inputs via jQuery is (in my opinion) is the one that You are using. And for the validation via jQuery i managed to find this: http://plugins.jquery.com/validate

It is poorly described in DOCs but from demos included in download You can figure things out. It also has some masking features but unfortunetly not as good as plugin You are using now.

As a result of input in masked field You should get ONLY values that are specified in mask. This plugin do not allow user to place other chars then they should give. Bare in mind that this plugin not always gets regular expressions right, thos can throw errors in pligun itself :/ and then You have a messed up mask due to JS error, start looking on that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top