最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regex - Javascript regular expression to match number Zero OR any whole number greater than zero - Stack Overflow

programmeradmin0浏览0评论

I have searched for hours but can't find this, please help. I need a Javascript regular expression to validate a text field that will contain a quantity value. The value can be zero OR any whole number greater than zero. Numbers cannot start with a zero unless they are... zero. It should not match if there is any whitespace at the start, middle or end of the quantity "string".

In other words: "0" <- Should match. "467" <- Should match. "098" <- Should not match. "1 3" <- Should not match.

So far I have to use two Reg Exps and OR them in Javascript. It is working. Here is my code:

function qtyIsValid(strQty){
    var isValid;
    var reg1 = new RegExp('^0{1}$'); // <-- This matches a single ZERO and fails if any whitespace anywhere
    var reg2 = new RegExp('^[1-9]+$'); // <-- This matches any number greater than zero and fails if any whitespace anywhere

    if ( reg1.test(strQty) || reg2.test(strQty) ){
            isValid = true;
    }else{
            isValid = false;
        }
return isValid;  
}

But can those two Regular Expressions be bined in to one within Javascript?

Thank you.

I have searched for hours but can't find this, please help. I need a Javascript regular expression to validate a text field that will contain a quantity value. The value can be zero OR any whole number greater than zero. Numbers cannot start with a zero unless they are... zero. It should not match if there is any whitespace at the start, middle or end of the quantity "string".

In other words: "0" <- Should match. "467" <- Should match. "098" <- Should not match. "1 3" <- Should not match.

So far I have to use two Reg Exps and OR them in Javascript. It is working. Here is my code:

function qtyIsValid(strQty){
    var isValid;
    var reg1 = new RegExp('^0{1}$'); // <-- This matches a single ZERO and fails if any whitespace anywhere
    var reg2 = new RegExp('^[1-9]+$'); // <-- This matches any number greater than zero and fails if any whitespace anywhere

    if ( reg1.test(strQty) || reg2.test(strQty) ){
            isValid = true;
    }else{
            isValid = false;
        }
return isValid;  
}

But can those two Regular Expressions be bined in to one within Javascript?

Thank you.

Share Improve this question asked Jan 16, 2017 at 0:50 FlexMcMurphyFlexMcMurphy 5435 silver badges25 bronze badges 3
  • 1 you could use parseInt(strQty) >= 0 && parseInt(strQty).toString() == strQty – Jaromanda X Commented Jan 16, 2017 at 0:52
  • 1 As implicitly said @JaromandaX, because you care about the value of the number you're matching, you better convert it to an integer. – Mathieu Paturel Commented Jan 16, 2017 at 0:57
  • I tried your suggestion in my code. Although this doesn't use Reg Exp, it is a nice solution that seems to work for my needs. It catches whitespace, non numeric characters and a number preceded by a zero. Awesome. – FlexMcMurphy Commented Jan 16, 2017 at 19:23
Add a ment  | 

2 Answers 2

Reset to default 4

You can use this for example:

r = /^(0|[1-9]\d*)$/
console.log("0".match(r));
console.log("467".match(r));
console.log("098".match(r));
console.log("1 3".match(r));

/^(0+|[1-9]\d*)$/ any string that is just a sequence of 0 or that contain a number not equal to 0 and not starting with 0.

console.log("0: ", /^(0+|[1-9]\d*)$/.test("0"));
console.log("000: ", /^(0+|[1-9]\d*)$/.test("000"));
console.log("055: ", /^(0+|[1-9]\d*)$/.test("055"));
console.log("123: ", /^(0+|[1-9]\d*)$/.test("123"));
console.log("1 3: ", /^(0+|[1-9]\d*)$/.test("1 3"));

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论