0

数量の正規表現を書きましたが、注文できる最大数はそれぞれ (夏、秋、冬) 500 です。式は -

 var chkquantity = ^([1-9]?\d|[1-4]\d{2}|500)$/ 

テストが正しいかどうかはわかりません -

if ((chkquantity.test(quantity)== false){
  alertmsg = alertmsg + "Please limit each magazine to 500 copies or less:" + "\n";
} 

?

4

3 に答える 3

1

数字を扱う場合は、数字として扱います。

if (Number(quantity) > 500) {
    alertmsg = alertmsg + "Please limit each magazine to 500 copies or less:" + "\n";
}
于 2013-02-15T17:43:51.337 に答える
1

Torsten の回答から構築すると、おそらくそれが数値であることも検証する必要があります。

if(Number(quantity) > 500 || isNaN(quantity)) {
于 2013-02-15T17:48:00.867 に答える
0

数字を扱っているので、より単純なアプローチを使用して逃げることができます。それがすべて数字であり、数値が500以下であることを検証するだけです

function checkLimit(val){
    if (!isNaN(val) && parseInt(val)<500)
        return true;
    else if(isNaN(val)){
        alert ("Enter valid quantity.");

    return false;
}

if (!checkLimit(quantity))
    alertmsg = alertmsg + "Please limit each magazine to 500 copies or less:" + "\n";
于 2013-02-15T17:56:14.567 に答える