admin管理员组文章数量:1022688
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
2 Answers
Reset to default 4You 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"));
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
2 Answers
Reset to default 4You 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"));
版权声明:本文标题:regex - Javascript regular expression to match number Zero OR any whole number greater than zero - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745522090a2154357.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论