admin管理员组文章数量:1025202
I need a regular expression in JavaScript that will accept only positive numbers and decimals ending with .5 or .0 .
(^\d*\.?\d*[1-9]+\d*$)|(^[1-9]+\d*\.\d*$)
This is what I have but I don't know how to change it to reject 0 and 0.0, and reject those ending with .1 .2 .3 .4 .6 .7 .8 .9
Accept:
0.5
1.0
1.5
2.0
2.5
3.0
1
2
3
4
But
Reject:
-1.0
-0.5
-0.0
-0.1
-0.7
0
0.0
0.1
0.9
0.4
1.3
1.11
1.51
2.01
I need a regular expression in JavaScript that will accept only positive numbers and decimals ending with .5 or .0 .
(^\d*\.?\d*[1-9]+\d*$)|(^[1-9]+\d*\.\d*$)
This is what I have but I don't know how to change it to reject 0 and 0.0, and reject those ending with .1 .2 .3 .4 .6 .7 .8 .9
Accept:
0.5
1.0
1.5
2.0
2.5
3.0
1
2
3
4
But
Reject:
-1.0
-0.5
-0.0
-0.1
-0.7
0
0.0
0.1
0.9
0.4
1.3
1.11
1.51
2.01
Share
Improve this question
edited Jul 7, 2016 at 6:58
user663031
asked Jul 7, 2016 at 3:35
Jbisgood9999999Jbisgood9999999
2531 gold badge5 silver badges15 bronze badges
8
-
Solution from Shay is great
^\d*\.\d{0,}(0|5)
I forget to include integer in the question So the final answer would be(^\d*\.\d{0,}(0|5))|(\d*)
– Jbisgood9999999 Commented Jul 7, 2016 at 3:51 - 1 Numeric checks should be done with numeric arithmetic, not regexps. Why do you specify the use of a regexp in your question? The correct title would be "Way to check for positive number and decimals (only end with .5 .0 or integer accepted)". – user663031 Commented Jul 7, 2016 at 3:57
-
The regexp you claim is correct matches
1.2345
, yet your question says the "decimal should end with.5
or.0
. Does the 5 or 0 need to e immediately after the decimal point, or at the end of a string of digits of any length after the decimal point? Whichever it is, the decimal portion would better be given as an optional portion after the digits before the decimal point, as in^\d*(\.\d{0,}(0|5))?
so as not to repeat the initial\d*
. This regexp also matches ".5"; is that something you want to accept? Is1.
valid too? The regexp you claim is correct also matches "1.59". – user663031 Commented Jul 7, 2016 at 4:07 - Oh yes, only 5 or 0 need to e immediately after the decimal point. Only 1 decimal point is accepted. Seems like "1.59" still can pass the Validation. – Jbisgood9999999 Commented Jul 7, 2016 at 4:11
- Seems like "1.59" still can pass the Validation. Do you mean that it passes this regexp but should not, or that it passes this regexp and that accepting it is correct? In the latter case, the phrasing "ends with" in your question is misleading. – user663031 Commented Jul 7, 2016 at 4:12
4 Answers
Reset to default 4Multiply the number by 2 and see if it is an integer:
function check(v) {
return v > 0 && !(v*2 % 1);
}
var tests = [
0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 1, 2, 3, 4,
-1.0, -0.5, -0.0, -0.1, -0.7, 0.0, 0.1, 0.9, 0.4, 1.3, 1.11, 1.51, 2.01];
tests.forEach(v => console.log(v, check(v) ? "passes": "fails"));
You can check this here at regex101. The regex I used to do this is:
(^0\.5$)|(^[1-9][0-9]*(\.[05])?$)
What is highlighted in blue is a match.
How about the following regular expression: ^\d*\.\d{0,}(0|5)
?
Here is the explanation:
^ //line start
\d* //any number (or none) occurrences of any number
\. //decimal dot
\d{0,} //0 or more digits (actually the same as * - you can use it here too)
(0|5) //ends with 0 or 5
Try it :
<html>
<head>
</head>
<body>
<script>
var patt = /^(\+)?\d+(\.[05])?$/;
var number = 1.5;
if (number > 0 && patt.test(number))
alert("Number is valid");
else
alert("Number is Invalid");
</script>
</body>
</html>
I need a regular expression in JavaScript that will accept only positive numbers and decimals ending with .5 or .0 .
(^\d*\.?\d*[1-9]+\d*$)|(^[1-9]+\d*\.\d*$)
This is what I have but I don't know how to change it to reject 0 and 0.0, and reject those ending with .1 .2 .3 .4 .6 .7 .8 .9
Accept:
0.5
1.0
1.5
2.0
2.5
3.0
1
2
3
4
But
Reject:
-1.0
-0.5
-0.0
-0.1
-0.7
0
0.0
0.1
0.9
0.4
1.3
1.11
1.51
2.01
I need a regular expression in JavaScript that will accept only positive numbers and decimals ending with .5 or .0 .
(^\d*\.?\d*[1-9]+\d*$)|(^[1-9]+\d*\.\d*$)
This is what I have but I don't know how to change it to reject 0 and 0.0, and reject those ending with .1 .2 .3 .4 .6 .7 .8 .9
Accept:
0.5
1.0
1.5
2.0
2.5
3.0
1
2
3
4
But
Reject:
-1.0
-0.5
-0.0
-0.1
-0.7
0
0.0
0.1
0.9
0.4
1.3
1.11
1.51
2.01
Share
Improve this question
edited Jul 7, 2016 at 6:58
user663031
asked Jul 7, 2016 at 3:35
Jbisgood9999999Jbisgood9999999
2531 gold badge5 silver badges15 bronze badges
8
-
Solution from Shay is great
^\d*\.\d{0,}(0|5)
I forget to include integer in the question So the final answer would be(^\d*\.\d{0,}(0|5))|(\d*)
– Jbisgood9999999 Commented Jul 7, 2016 at 3:51 - 1 Numeric checks should be done with numeric arithmetic, not regexps. Why do you specify the use of a regexp in your question? The correct title would be "Way to check for positive number and decimals (only end with .5 .0 or integer accepted)". – user663031 Commented Jul 7, 2016 at 3:57
-
The regexp you claim is correct matches
1.2345
, yet your question says the "decimal should end with.5
or.0
. Does the 5 or 0 need to e immediately after the decimal point, or at the end of a string of digits of any length after the decimal point? Whichever it is, the decimal portion would better be given as an optional portion after the digits before the decimal point, as in^\d*(\.\d{0,}(0|5))?
so as not to repeat the initial\d*
. This regexp also matches ".5"; is that something you want to accept? Is1.
valid too? The regexp you claim is correct also matches "1.59". – user663031 Commented Jul 7, 2016 at 4:07 - Oh yes, only 5 or 0 need to e immediately after the decimal point. Only 1 decimal point is accepted. Seems like "1.59" still can pass the Validation. – Jbisgood9999999 Commented Jul 7, 2016 at 4:11
- Seems like "1.59" still can pass the Validation. Do you mean that it passes this regexp but should not, or that it passes this regexp and that accepting it is correct? In the latter case, the phrasing "ends with" in your question is misleading. – user663031 Commented Jul 7, 2016 at 4:12
4 Answers
Reset to default 4Multiply the number by 2 and see if it is an integer:
function check(v) {
return v > 0 && !(v*2 % 1);
}
var tests = [
0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 1, 2, 3, 4,
-1.0, -0.5, -0.0, -0.1, -0.7, 0.0, 0.1, 0.9, 0.4, 1.3, 1.11, 1.51, 2.01];
tests.forEach(v => console.log(v, check(v) ? "passes": "fails"));
You can check this here at regex101. The regex I used to do this is:
(^0\.5$)|(^[1-9][0-9]*(\.[05])?$)
What is highlighted in blue is a match.
How about the following regular expression: ^\d*\.\d{0,}(0|5)
?
Here is the explanation:
^ //line start
\d* //any number (or none) occurrences of any number
\. //decimal dot
\d{0,} //0 or more digits (actually the same as * - you can use it here too)
(0|5) //ends with 0 or 5
Try it :
<html>
<head>
</head>
<body>
<script>
var patt = /^(\+)?\d+(\.[05])?$/;
var number = 1.5;
if (number > 0 && patt.test(number))
alert("Number is valid");
else
alert("Number is Invalid");
</script>
</body>
</html>
本文标签:
版权声明:本文标题:javascript - Regular Expression to accept only positive number and decimals (only end with .5 .0 or integer accepted) - Stack Ov 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745612419a2159083.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论