Masked Input Plugin and regular expressions (for d

2019-06-03 06:20发布

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?

3条回答
淡お忘
2楼-- · 2019-06-03 06:36

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>
查看更多
在下西门庆
3楼-- · 2019-06-03 06:52

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.

查看更多
干净又极端
4楼-- · 2019-06-03 07:03

Try this,

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

Working Demo

查看更多
登录 后发表回答