在jquery中表示正小数值和-1值的正则表达式

时间:2022-09-15 21:41:34

How do a Regular expression for positive decimal value and -1 value in jquery? I managed to do it for positive and negative decimal value with this, but it can only be -1. Any idea?

如何在jquery中对正十进制值和-1值进行正则表达式?我用这个求出了正小数和负小数,但它只能是-1。任何想法?

$(".SermeCoopValidarTope").keypress(function (e) {
    var tecla = (document.all) ? e.keyCode : e.which;
    var numeroDecimal = $(this).val();
    if (tecla == 8) return true;

    if (tecla > 47 && tecla < 58) {
        if (numeroDecimal == "") return true
        regexp = /^([0-9])*[.]?[0-9]{0,1}$/;
        return (regexp.test(numeroDecimal))
    }
    if (tecla == 46) {
        if (numeroDecimal == "") return false
        regexp = /^[0-9]+$/
        return regexp.test(numeroDecimal)
    }
    return false
});

2 个解决方案

#1


2  

Use or | with two matching expressions to test for either/or match.

使用或|与两个匹配表达式一起测试/或匹配。

I also re-wrote the code to construct the expected value, based on current value and new keypress. This simplifed the code a lot.

我还重新编写了代码,以基于当前值和新的按键来构造预期值。这大大简化了代码。

$(".SermeCoopValidarTope").keypress(function (e) {
    var tecla = (document.all) ? e.keyCode : e.which;

    var numeroDecimal = $(this).val();

    // Allow backspace
    if (tecla == 8) return true;

    // if it's a valid character, append it to the value
    if ((tecla > 47 && tecla < 58) || tecla == 45 || tecla == 46) {
        numeroDecimal += String.fromCharCode(tecla)
    }
    else return false;

    // Now test to see if the result "will" be valid (if the key were allowed)

    regexp = /^\-1?$|^([0-9])*[.]?[0-9]{0,2}$/;
    return (regexp.test(numeroDecimal));
});

JSFiddle: http://jsfiddle.net/TrueBlueAussie/Ld3n4b56/

JSFiddle:http://jsfiddle.net/TrueBlueAussie/Ld3n4b56/

Update to support , instead of . for the decimal separator:

更新以支持,而不是。十进制分隔符:

$(".SermeCoopValidarTope").keypress(function (e) {
    var tecla = (document.all) ? e.keyCode : e.which;

    var numeroDecimal = $(this).val();

    // Allow backspace
    if (tecla == 8) return true;

    // if it's a valid character, append it to the value
    if ((tecla > 47 && tecla < 58) || tecla == 45 || tecla == 44) {
        numeroDecimal += String.fromCharCode(tecla)
    }
    else return false;

    // Now test to seee of the result will be valid

    regexp = /^\-1?$|^([0-9])*[,]?[0-9]{0,2}$/;
    return (regexp.test(numeroDecimal));
});

JSFiddle: http://jsfiddle.net/TrueBlueAussie/Ld3n4b56/1/

JSFiddle:http://jsfiddle.net/TrueBlueAussie/Ld3n4b56/1/

Shortened versions of the RegEx (thanks to @Brian Stephens):

RegEx的缩略版(感谢@Brian Stephens):

Period decimal separator: http://jsfiddle.net/Ld3n4b56/4/

期十进制分隔符:http://jsfiddle.net/Ld3n4b56/4/

/^(-1?|\d*.?\d{0,2})$/

Comma decimal separator: http://jsfiddle.net/Ld3n4b56/3/

逗号十进制分隔符:http://jsfiddle.net/Ld3n4b56/3/

/^(-1?|\d*,?\d{0,2})$/

#2


1  

You can use | (or operator):

可以使用|(或操作符):

/^([0-9]+|-1)$/ or simply /^(\d+|-1)$/

Also I suggest you to change your regex /^([0-9])*[.]?[0-9]{0,1}$/ to

我建议你改变你的正则表达式/ ^([0 - 9])*(。)?[0 - 9]{ 0,1 } /美元

/^([0-9])*(\.[0-9])?$/ or simply /^\d*(\.\d)?$/

For making it more meaningful and to not allow values like 123. (ending with dot) or just .

使它更有意义,不允许像123这样的值。(以点结尾)或者。

#1


2  

Use or | with two matching expressions to test for either/or match.

使用或|与两个匹配表达式一起测试/或匹配。

I also re-wrote the code to construct the expected value, based on current value and new keypress. This simplifed the code a lot.

我还重新编写了代码,以基于当前值和新的按键来构造预期值。这大大简化了代码。

$(".SermeCoopValidarTope").keypress(function (e) {
    var tecla = (document.all) ? e.keyCode : e.which;

    var numeroDecimal = $(this).val();

    // Allow backspace
    if (tecla == 8) return true;

    // if it's a valid character, append it to the value
    if ((tecla > 47 && tecla < 58) || tecla == 45 || tecla == 46) {
        numeroDecimal += String.fromCharCode(tecla)
    }
    else return false;

    // Now test to see if the result "will" be valid (if the key were allowed)

    regexp = /^\-1?$|^([0-9])*[.]?[0-9]{0,2}$/;
    return (regexp.test(numeroDecimal));
});

JSFiddle: http://jsfiddle.net/TrueBlueAussie/Ld3n4b56/

JSFiddle:http://jsfiddle.net/TrueBlueAussie/Ld3n4b56/

Update to support , instead of . for the decimal separator:

更新以支持,而不是。十进制分隔符:

$(".SermeCoopValidarTope").keypress(function (e) {
    var tecla = (document.all) ? e.keyCode : e.which;

    var numeroDecimal = $(this).val();

    // Allow backspace
    if (tecla == 8) return true;

    // if it's a valid character, append it to the value
    if ((tecla > 47 && tecla < 58) || tecla == 45 || tecla == 44) {
        numeroDecimal += String.fromCharCode(tecla)
    }
    else return false;

    // Now test to seee of the result will be valid

    regexp = /^\-1?$|^([0-9])*[,]?[0-9]{0,2}$/;
    return (regexp.test(numeroDecimal));
});

JSFiddle: http://jsfiddle.net/TrueBlueAussie/Ld3n4b56/1/

JSFiddle:http://jsfiddle.net/TrueBlueAussie/Ld3n4b56/1/

Shortened versions of the RegEx (thanks to @Brian Stephens):

RegEx的缩略版(感谢@Brian Stephens):

Period decimal separator: http://jsfiddle.net/Ld3n4b56/4/

期十进制分隔符:http://jsfiddle.net/Ld3n4b56/4/

/^(-1?|\d*.?\d{0,2})$/

Comma decimal separator: http://jsfiddle.net/Ld3n4b56/3/

逗号十进制分隔符:http://jsfiddle.net/Ld3n4b56/3/

/^(-1?|\d*,?\d{0,2})$/

#2


1  

You can use | (or operator):

可以使用|(或操作符):

/^([0-9]+|-1)$/ or simply /^(\d+|-1)$/

Also I suggest you to change your regex /^([0-9])*[.]?[0-9]{0,1}$/ to

我建议你改变你的正则表达式/ ^([0 - 9])*(。)?[0 - 9]{ 0,1 } /美元

/^([0-9])*(\.[0-9])?$/ or simply /^\d*(\.\d)?$/

For making it more meaningful and to not allow values like 123. (ending with dot) or just .

使它更有意义,不允许像123这样的值。(以点结尾)或者。