Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 数字和十进制的输入掩码_Javascript_Jquery_Html_Regex_Input Mask - Fatal编程技术网

Javascript 数字和十进制的输入掩码

Javascript 数字和十进制的输入掩码,javascript,jquery,html,regex,input-mask,Javascript,Jquery,Html,Regex,Input Mask,在测试我的程序后,我实现了软件应用程序管理开票。我注意到以下错误: sqlserver中的我的表包含:价格数字(6,2) 我的程序的用户输入价格为555.00是好的。 但是当他输入555555时,它是错误的,所以我需要指定掩码,其中尾数是可选的0到999,小数部分是可编程的2或3,根据用户的选择,我使用的是JQuery掩码输入插件,我没有找到好的正则表达式,请帮帮忙,我正在使用jsp/servlet。您可以用于数字。 当前版本确实允许您查找的内容,但有人有一点代码,并且可以正常工作: HTML

在测试我的程序后,我实现了软件应用程序管理开票。我注意到以下错误: sqlserver中的我的表包含:价格数字(6,2) 我的程序的用户输入价格为555.00是好的。 但是当他输入555555时,它是错误的,所以我需要指定掩码,其中尾数是可选的0到999,小数部分是可编程的2或3,根据用户的选择,我使用的是JQuery掩码输入插件,我没有找到好的正则表达式,请帮帮忙,我正在使用jsp/servlet。

您可以用于数字。
当前版本确实允许您查找的内容,但有人有一点代码,并且可以正常工作:

HTML

<input class="numeric" type="text" />
这就是全部。

我已经准备好了这个,这样你就可以看到它是如何工作的。

现在我已经更好地理解了你需要什么,下面是我的建议。为文本框添加一个keyup处理程序,该处理程序使用以下正则表达式检查文本框内容:regex
^[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');
        }
    });
});
下面是一个正在运行的示例。另外,在服务器端执行相同的正则表达式,如果不匹配,则说明没有满足要求。您还可以检查onsubmit事件,如果正则表达式不匹配,则不允许用户提交页面


在插入文本时不强制使用掩码的原因是,这会使事情复杂化很多,例如,正如我在评论中提到的,用户无法开始输入有效的输入,因为输入无效。虽然这是可能的,但我建议改为使用jQuery输入掩码插件(6位整数和2位小数):

HTML:


我希望这对某人有所帮助

您可以使用jquery插件来完成

HTML:

或者

<input type="text" onkeypress="handleNumber(event, '€ {-10,3} $')" placeholder="€  $" size=25>

函数句柄编号(事件、掩码){
/*带有前置、后置、减号、点和逗号作为小数分隔符的数字掩码
{}:正整数
{10} :最大为10位的正整数
{,3}:正浮点最多3位小数
{10,3}:正浮点最大值为7位和3位小数
{null,null}:正整数
{10,null}:正整数,最多10位
{null,3}:正浮点最大值为3十进制
{-}:正整数或负整数
{-10}:正整数或负整数最多10位
{-,3}:正浮点数或负浮点数最多3位小数
{-10,3}:正或负浮点最大值为7位和3位小数
*/
有(事件){
停止传播()
预防默认值()
如果(!charCode)返回
var c=String.fromCharCode(charCode)
if(c.match(/[^-\d,]/)返回
有(目标){
var txt=value.substring(0,selectionStart)+c+value.substr(selectionEnd)
var pos=选择开始+1
}
}
变量点=计数(txt,/\./,位置)
txt=txt.replace(/[^-\d,]/g,,)
var mask=mask.match(/^(\D*)\{(-)(\D*| null)?(?:,(\D+| null))?\}(\D*)$/);如果(!mask)返回//meglio异常?
变量符号=!!掩码[2],小数=+掩码[4],整数=数学最大值(0,+掩码[3]-(小数| | 0))
如果(!txt.match(“^”+(!sign?“”:“-?”)+“\\d*”+(!decimals?“”:(,\\d*)?”)+“$”)返回
txt=txt.split(',')
if(integers&&txt[0]&&count(txt[0],/\d/)>integers)返回
if(小数和&txt[1]&&txt[1]。长度>小数)返回
txt[0]=txt[0]。替换(/\B(?=(\d{3})+(?!\d))/g,'.)
使用(event.target){
value=mask[1]+txt.join(',')+mask[5]
selectionStart=selectionEnd=pos+(pos==1?掩码[1]。长度:计数(值,/\./,pos)-点)
}
函数计数(str、c、e){
e=e | | str.length

对于(var n=0,i=0;i尝试。它有Number、RegExp和其他掩码。扩展非常简单。

使用tow函数来解决它,非常简单和有用:

HTML:


如果您的系统是英文的,请使用@Rick answer:

如果您的系统是巴西葡萄牙语,请使用以下命令:

进口:

<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>
这是因为在巴西葡萄牙语中,我们写的是“1.000.000,00”,而不是英语中的“1000000.00”,因此如果使用“.”系统将无法理解小数点


就是它,我希望它能帮助别人。我花了很多时间来理解它。

Ehm…如果数据库字段是数字(6,2),那么小数位数不能是“根据用户的选择2或3”。我缺少什么?是的,我在数据库(6,3)中更改了它,小数点2或3仅在显示中是可选的。但问题是sameI正在尝试使用正则表达式,但正则表达式错误,我收到一个错误。var ex=/[0-9]{1-3}\.[0-9]{2}我想在输入小数点值时不放两个0。请帮助始终在服务器端和客户端(javascript)上进行验证只是为了可用性,但不要依赖它。现在,如果我理解正确的话,你希望能够输入0到999,带或不带2或3个小数?因为我不确定尾数的含义,因为我在谷歌搜索的内容与你描述的内容并不匹配。关于最后一条评论,请输入“00”通过验证,您希望它不通过,对吗?我在这里尝试了正则表达式:/(\d{1,14})(\。\d{1,3}){0,1}$/(regex)是假的。我需要你的帮助。我只需要将点之前的数字强制为14个数字的最大值,就像这样:0.000到9999999999999.9999当我使用精度4时,我可以输入到9999,我想要的精度4是99。99@najeh22:自定义插件。不幸的是jquery.numeric不适用。在最近的浏览器中,会生成大量错误量。/^[0-9]{1,14}\.[0-9]{2}$/不正确,有点错误,因为我可以在这里输入我想要的数字,字母tooI使用这个,但是ex不正确函数checkDec(el){var ex=/^[0-9]{1,3}.[0-9]{2}$/;if(ex.test(el.value)==false){el.value=el.value=el.value.substring(0,el.value.length-1);}@najeh22你可以随意进入,
$(".mask").inputmask('Regex', {regex: "^[0-9]{1,6}(\\.\\d{1,2})?$"});
<input id="price" type="text">
$('#price').inputmask({
  alias: 'numeric', 
  allowMinus: false,  
  digits: 2, 
  max: 999.99
});
<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
    }
}
<input class="int-number" type="text" />
<input class="decimal-number" type="text" />
//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');
});
<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>
<input class="mask" type="text" />
$(".mask").inputmask('Regex', {regex: "^[0-9]{1,6}(\\,\\d{1,2})?$"});