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

javascript - Regular Expression to accept only positive number and decimals (only end with .5 .0 or integer accepted) - Stack Ov

programmeradmin3浏览0评论

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? Is 1. 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
 |  Show 3 more ments

4 Answers 4

Reset to default 4

Multiply 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>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论