最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin4PV0评论

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>

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far