1

I want to check if input text (amount) is between 1-100 or not but regex test not working.

here is my JS code

<script type="text/javascript" >    

    console.log(document.getElementById("amount").value); // 222

    var regex = /^(100)|[1-9]\d?$/;
    if(regex.test(document.getElementById("amount").value))
    {
        alert('validated')
    }
    else
        alert('error')

</script>
2
  • 1
    You have to put the stuff between the anchors into a (non capturing) group, otherwise the anchors are part of the alternation. Commented Jul 5, 2016 at 8:20
  • convert value to number and compare normally, regex may not be suitable in your scenario. Commented Jul 5, 2016 at 8:21

4 Answers 4

4
  1. Wrap the code in DOMContentLoaded event callback
  2. Don't use RegEx. Use comparison operators

Code:

document.addEventListener('DOMContentLoaded', function () {
    // Get the value & convert it into Number
    var value = parseInt(document.getElementById('amount').value, 10);

    // Value between 0 & 100
    if (value > 0 && value <= 100) {
        alert('Valid');
    } else {
        alert('Error');
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

This doesn't actually do the same check as the original requirement. For instance, it will allow -100 and 0, and it will alert "Valid" for entries like '2sdf' and "0x12". But it's almost certainly closer to what you want than using regex.
@Chris Lear, yes! i do it myself and of course i deleted many lines of code to get it simple. i just looking to find a good way to do it in JS. Thanks!
1

It would be enough to use parseInt() function or Number constructor to perform such a simple validation:

<script type="text/javascript" >
    var amount = Number(document.getElementById("amount").value); // 222
    alert ((amount > 0 && amount <= 100)? 'Valid' : 'Invalid');
</script> 

5 Comments

This answer also forgets to check that the entry is more than 0, and doesn't check for integers either. Which might be correct... it's a little hard to be sure.
Fixed. Thanks. Besides, doesn't check for integers either - no need to check that, if it's not integer - it's invalid
What I meant was that this will say that 23.56 is valid, whereas the original regex would have failed that.
it's not a regex solution. The OP didn't specified all the "ideal" conditions. If you think that your regex is the best - let the OP accept your answer
I don't think my regex is best. I think the accepted answer is the best, as it happens. I was simply pointing out potential pitfalls. I don't think the OP did much checking. If it turns out that decimals are acceptable, then your answer might be better (especially if the actual code later uses the outpit of Number() or parseInt(), rather than just using it as a validation check).
0

If you really want to use regex, this should work:

/^100$|^[1-9]\d?$/

It doesn't allow eg 09, but maybe that's OK.

2 Comments

not work, Uncaught TypeError: Cannot read property 'show' of null
That error isn't anything to do with this regex, but if what you actually wanted was just to convert to an integer and compare with 100, then you're way better off doing that.
0
    <html>
    <head>

    <script>

    function validateValue()
    {
    var amount = document.getElementById('testId').value;
    if((isNaN(amount ))){
    document.getElementById("errorId").style.display= "";
    }
    else
    {
    if(amount>100){
    document.getElementById("errorId").style.display= "";
    }
    else
    {
    document.getElementById("errorId").style.display= "none";
    }
    }
    }

    </script>

    </head>
    <body>

    Enter 1 to 100:<input type="text" onkeyup="validateValue()"         id="testId"/>
    <span id="errorId" style="color:red;display:none"> Not a valid amount</span>
    </body>
    </html>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.