Masked Input Plugin and regular expressions (for decimal numbers)

荒凉一梦 提交于 2019-12-08 06:28:15

问题


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?


回答1:


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>



回答2:


Try this,

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

Working Demo




回答3:


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.



来源:https://stackoverflow.com/questions/17995998/masked-input-plugin-and-regular-expressions-for-decimal-numbers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!