1

I have the regex that accept number between 1 and 100. But it accepting the special character '%'

This is the regex that I am using

^(0*100{1,1}\\.?((?<=\\.)0*)?%?$)|(^0*\\d{0,2}\\.?((?<=\\.)\\d*)?%?)$'

I dont want to accept % Can anyone please help me in fixing this.

user2028437
  • 13
  • 1
  • 1
  • 4

8 Answers8

7

The regular expression suggested by Ali Shah Ahmed doesn't work with 1 digit numbers, this one does:

(100)|(0*\d{1,2})

Edited: If you don't want to accept the value 0 you can use this regular expression:

(100)|[1-9]\d?
Community
  • 1
  • 1
Rui Jarimba
  • 11,166
  • 11
  • 56
  • 86
  • 2
    this accepts `1000` in JS, `(^100$)|^[1-9]\d?$` is 1-100 – vbranden Jun 22 '16 at 16:47
  • @vbranden 2nd regex is valid in javascript. – Rui Jarimba Jun 22 '16 at 19:17
  • its valid but it allows 1000 which is outside the bounds of the OP's requirement that it only be 1 - 100 https://regex101.com/r/vS0pY7/1 – vbranden Jun 22 '16 at 19:18
  • @vbranden sorry but I don't understand - I tried the regex in 3 different javascript regex testers and all matched `100`, not `1000`. Last zero is not matched. – Rui Jarimba Jun 22 '16 at 19:26
  • put it this way, if you do `if (numberString.match(/(100)|[1-9]\d?/)) { myFunction() }` and the numberString is `1000` then `myFunction` will be called. because it only matches 100 does not mean that other numbers are no accepted. how would the user know if `numberString` is between 1 and 100 if numbers 1 - infinity are matched? – vbranden Jun 22 '16 at 19:30
  • Obviously if you need the validate the whole string you might need to use the start and end delimiters - see http://stackoverflow.com/questions/6298566/regex-match-whole-string – Rui Jarimba Jun 22 '16 at 19:33
  • @vbranden this is probably what you need: http://stackoverflow.com/a/447258/558486 – Rui Jarimba Jun 22 '16 at 19:51
  • @vbranden I think what you need is http://stackoverflow.com/a/42060770/1736092 which would not match for `I have 1000 dollars` in your example and includes samples for JavaScript – spex Feb 06 '17 at 05:36
1

Remove all the %?. This indicates that the regex should match zero or one % characters.

orique
  • 1,295
  • 1
  • 27
  • 36
1

I would take a multi-step approach. Wouldn't try and solve this with a regex alone. The maintenance is a nightmare.

My solution:-

Use a simple regex to determine whether the value is an integer with three or less digits.

/^\d{1,3}$/

If valid, cast to a number.

Check to see that the number is <= 100 and >= 0.

Paul Alan Taylor
  • 10,474
  • 1
  • 26
  • 42
1

i believe this regex will also work, and is bit simpler than the one you mentioned.

(100)|(0*\d{1,2})

this will take care of leading zeros as well.

Ali Shah Ahmed
  • 3,263
  • 3
  • 24
  • 22
1

This matches only 1 to 100 inclusive:
/\b((100)|[1-9]\d?)\b/

The \b matches word boundaries. This means something like in JavaScript:
/((100)|[1-9]\d?)/.test("I have 1000 dollars");
would return true. Instead, using the word boundaries would return false.

spex
  • 1,110
  • 10
  • 21
0

This allows numbers from 1 to 100:

100|[1-9]?\d
Naveed S
  • 5,106
  • 4
  • 34
  • 52
0

^(100|[1-9][0-9]?)$ This regular expression matches between 1 to 100, 100 matches exactly 100 or [1-9] matches a character in the range 1 to 9 [0-9] matches a character in the range 0 to 9 ? matches between 0 and 1 of the preceding token

and for your reference validate the regular expression combinations here https://regexr.com/

Harish Nayak
  • 348
  • 3
  • 18
0

Whatever others said is fine.

I am giving a complete and more precise code for the above asked question. Hope it helps.

    const onChange = (userInput) => {
    const regex = /^([1-9]|[1-9][0-9]|100)$/;
    if (userInput === "" || regex.test(userInput)) {
      console.log("valid number and in range of 1-100");
    } else {
      console.log("not a number and in range of 1-100");
    }
  };
Alok Ranjan
  • 967
  • 10
  • 10