2

Problem is: I have one text area in jsp file, from here I am reading value through javascript and validating if the entered value is a "number between 1-25". Below is my explanation.

1) My string should contain only number between [1-25]

2) It should not have any of the characters (spl chars/symbols) apart from numbers [1-25] at any position in the string.

for example:

 12-allowed  (less than 25 and greater than 1) show success_message
 1@#4- not allowed, show warn_message
 @14#- not allowed, show warn_message
 !$%12()*& - not allowed even though number is less than 25 and greater than 1, since it contains characters apart from numbers, show warn_message.

This is what i have tried

File.jsp

 <div class="row">
    <div class="large-1 columns">
        <input id="text_area" name="posArea" type="text"/>

    </div>
</div>

MyFile.js

 <script>
    var pos= ($('#text_area').val());

    var reg="^[1-25]$";

        if(pos.match(reg)){

                $('#warn_message').show();
                return false;
         }

         if(isNaN(pos)){

            $('#warn_message').show();
            return false;

            }
       else{

            $('#success_message').show();
       }
 </script>

This is showing warning message for any number I enter, Can anyone spot my mistake? Thanks for any help. Please comment in case you do not understand my question

user3534759
  • 221
  • 2
  • 12

3 Answers3

2
^(?:[1-9]|1\d|2[0-5])$

Try this.See demo.

https://regex101.com/r/sJ9gM7/17

[1-25] does not mean a range from 1 to 25.It means a range from 1 to 2 and 5 in a character class.

vks
  • 67,027
  • 10
  • 91
  • 124
  • your regex working fine in the link provided but in my program, it is showing error message for number between 1-25 also.. Please have look into my code where I missed. I tried but not able to spot the error. – user3534759 Mar 27 '15 at 09:22
  • @user3534759 `var re = /^(?:[1-9]|1\d|2[0-5])$/gm; ` – vks Mar 27 '15 at 09:23
  • This expression works but this will not remove content of text area even after refreshing browser.it was happening before but now when I refresh browser , text area will be having last entered value. – user3534759 Mar 27 '15 at 10:38
  • After long time I got to know about this ,I have one question in this.In case if you enter 06 as your input it should take ideally but its not taking since number prefixed with zero.how do I make it to work as 06 and 6 both should be correct. Thanks if you still remember and able to give suggestion – user3534759 Apr 30 '15 at 11:11
  • 1
    that worked!! and I understood how to make it as well.. Thanks – user3534759 Apr 30 '15 at 11:16
2

The regex [1-25] matches 1, 2, 5, 1 time exactly.

For whole numbers, you can use this regex: ^(?:2[0-5]|[1][0-9]|[1-9])$

It will not fractions, but I think you are not interested in them.

See demo.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • @stribizhev..Number should be only in range [1,2,3,4....25] not more than that and not even less than 1 as well. your regex accepts 0 as well.. – user3534759 Mar 27 '15 at 09:19
  • This regex will not allow `0`: `^(?:2[0-5]|[1][0-9]|[1-9])$`. I updated the answer, see example at https://regex101.com/r/lT7qP3/2. – Wiktor Stribiżew Mar 27 '15 at 09:22
  • @stribizhev..you are right. when I use this with method match(), what it returns when my input does not match with this regex? true?? – user3534759 Mar 27 '15 at 09:26
  • JS `match` returns the matches as an Array object. See http://www.w3schools.com/jsref/jsref_match.asp. To return boolean, use `regex.test()`, see http://stackoverflow.com/questions/6603015/javascript-check-a-string-matches-a-regex. – Wiktor Stribiżew Mar 27 '15 at 09:28
1

You can do that without Regex :

function checkNumber(number) {
  if(isNaN(number)) {
    return false;
  } else {
    number = parseInt(number)
    return number > 1 && number < 25
  }
}
jmgross
  • 2,306
  • 1
  • 20
  • 24