我在测试我的程序后,意识到需要开发一个软件应用管理发票的功能。我发现以下错误: 我的SQL Server表包含:价格(numeric(6,2)) 当用户输入555.00时没有问题。 但是当他输入555555时就会出现错误,因此我需要指定掩码,其中尾数为可选的0到999,小数部分根据用户选择可编程为2或3。我正在使用jQuery Masked input插件,但是我没有找到好的正则表达式,请帮帮我。我正在使用JSP/servlet。
我在测试我的程序后,意识到需要开发一个软件应用管理发票的功能。我发现以下错误: 我的SQL Server表包含:价格(numeric(6,2)) 当用户输入555.00时没有问题。 但是当他输入555555时就会出现错误,因此我需要指定掩码,其中尾数为可选的0到999,小数部分根据用户选择可编程为2或3。我正在使用jQuery Masked input插件,但是我没有找到好的正则表达式,请帮帮我。我正在使用JSP/servlet。
HTML
<input class="numeric" type="text" />
JQuery
$(".numeric").numeric({ decimal : ".", negative : false, scale: 3 });
使用 jQuery 输入掩码插件(6位整数和2位小数):
HTML:
使用jQuery输入掩码插件(6位整数和2位小数):
HTML:
<input class="mask" type="text" />
jQuery:
$(".mask").inputmask('Regex', {regex: "^[0-9]{1,6}(\\.\\d{1,2})?$"});
我希望这能帮助到某人。
您可以使用jQuery inputmask插件来实现此功能。
HTML:
<input id="price" type="text">
JavaScript:
$('#price').inputmask({
alias: 'numeric',
allowMinus: false,
digits: 2,
max: 999.99
});
'rightAlign': false
。这是我寻找已久的答案!非常感谢! - Gloria Chen<input class="int-number" type="text" />
<input class="decimal-number" type="text" />
JQuery:
//Integer Number
$(document).on("input", ".int-number", function (e) {
this.value = this.value.replace(/[^0-9]/g, '');
});
//Decimal Number
$(document).on("input", ".decimal-number", function (e) {
this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1');
});
<input type="text" onkeypress="handleNumber(event, '€ {-10,3} $')" placeholder="€ $" size=25>
使用
function handleNumber(event, mask) {
/* numeric mask with pre, post, minus sign, dots and comma as decimal separator
{}: positive integer
{10}: positive integer max 10 digit
{,3}: positive float max 3 decimal
{10,3}: positive float max 7 digit and 3 decimal
{null,null}: positive integer
{10,null}: positive integer max 10 digit
{null,3}: positive float max 3 decimal
{-}: positive or negative integer
{-10}: positive or negative integer max 10 digit
{-,3}: positive or negative float max 3 decimal
{-10,3}: positive or negative float max 7 digit and 3 decimal
*/
with (event) {
stopPropagation()
preventDefault()
if (!charCode) return
var c = String.fromCharCode(charCode)
if (c.match(/[^-\d,]/)) return
with (target) {
var txt = value.substring(0, selectionStart) + c + value.substr(selectionEnd)
var pos = selectionStart + 1
}
}
var dot = count(txt, /\./, pos)
txt = txt.replace(/[^-\d,]/g,'')
var mask = mask.match(/^(\D*)\{(-)?(\d*|null)?(?:,(\d+|null))?\}(\D*)$/); if (!mask) return // meglio exception?
var sign = !!mask[2], decimals = +mask[4], integers = Math.max(0, +mask[3] - (decimals || 0))
if (!txt.match('^' + (!sign?'':'-?') + '\\d*' + (!decimals?'':'(,\\d*)?') + '$')) return
txt = txt.split(',')
if (integers && txt[0] && count(txt[0],/\d/) > integers) return
if (decimals && txt[1] && txt[1].length > decimals) return
txt[0] = txt[0].replace(/\B(?=(\d{3})+(?!\d))/g, '.')
with (event.target) {
value = mask[1] + txt.join(',') + mask[5]
selectionStart = selectionEnd = pos + (pos==1 ? mask[1].length : count(value, /\./, pos) - dot)
}
function count(str, c, e) {
e = e || str.length
for (var n=0, i=0; i<e; i+=1) if (str.charAt(i).match(c)) n+=1
return n
}
}
<script
src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.15/jquery.mask.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.inputmask/3.2.6/jquery.inputmask.bundle.min.js"></script>
HTML:
<input class="mask" type="text" />
JS:
$(".mask").inputmask('Regex', {regex: "^[0-9]{1,6}(\\,\\d{1,2})?$"});
这是因为在巴西葡萄牙语中,我们写作“1.000.000,00”,而不像英语中的“1,000,000.00”,因此如果使用“.”,系统将无法理解小数点。
就是这样,我希望它能帮助到某些人。我花了很多时间才明白这个问题。
现在我更好地理解了您的需求,这是我的建议。为您的文本框添加一个keyup处理程序,检查文本框内容是否与此正则表达式匹配:^[0-9]{1,14}\.[0-9]{2}$
,如果不匹配,则将背景设置为红色或显示文本或其他您喜欢的方式。以下是要放在document.ready中的代码:
$(document).ready(function() {
$('selectorForTextbox').bind('keyup', function(e) {
if (e.srcElement.value.match(/^[0-9]{1,14}\.[0-9]{2}$/) === null) {
$(this).addClass('invalid');
} else {
$(this).removeClass('invalid');
}
});
});
这里是一个 JSFiddle 的演示。同时,也要在服务器端执行相同的正则表达式,如果没有匹配,说明要求未满足。您还可以在 onsubmit 事件中进行检查,如果正则表达式不匹配,则不让用户提交页面。
不强制插入文本时使用掩码的原因是它会使事情变得非常复杂,例如我在评论中提到的那样,用户无法开始输入有效的输入,因为它的开头不是有效的。虽然这是可能的,但我建议使用这种方法代替。