Alpesh003
Alpesh003

Reputation: 329

Need to accept only % symbol as an input string

I want to accept only % symbol(optional) as a string variable with a number, as in input as percentage. E.G, 1000% or 100% or maybe 2000% or plain string input as 1000 or 2000.

Can we do it with help of a regular expression? or maybe some other validation.

Please help

Upvotes: 2

Views: 190

Answers (5)

Jayendra
Jayendra

Reputation: 52799

You can try with the digits and % as an optional character.

^\d+(\.\d+)?%?$

Upvotes: 0

Tim Pietzcker
Tim Pietzcker

Reputation: 336368

^[0-9]+%?$

assuming that at least one digit is required and only positive numbers (or 0) are allowed.

If you also want to allow decimals (you could have mentioned that in your question), try

^[0-9]+(?:\.[0-9]+)?%?$

This allows any positive integer or decimal (but not abbreviated forms like .1 or 1.). Exponential notation is of course not supported.

Upvotes: 2

FailedDev
FailedDev

Reputation: 26930

Based on this comment :

it seems to work perfect with patterns like 1000%, 100 but now problems with decimal values like 101.50 or 199.50%

^[-+]?\d*\.?\d+\b%?$

Explanation :

"
^        # Assert position at the beginning of the string
[-+]     # Match a single character present in the list “-+”
   ?        # Between zero and one times, as many times as possible, giving back as needed (greedy)
[0-9]    # Match a single character in the range between “0” and “9”
   *        # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\.       # Match the character “.” literally
   ?        # Between zero and one times, as many times as possible, giving back as needed (greedy)
[0-9]    # Match a single character in the range between “0” and “9”
   +        # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
\b       # Assert position at a word boundary
%        # Match the character “%” literally
   ?        # Between zero and one times, as many times as possible, giving back as needed (greedy)
\$        # Assert position at the end of the string (or before the line break at the end of the string, if any)
"

As a side note : You should really have been able to account for the optional part from @ Tim's answer. Take a look at the tutorial proposed by the other answers.

Upvotes: 0

curial
curial

Reputation: 514

Try the following regex

^[+-]?\d+(\.\d+)?%?$

Should work with decimals

Upvotes: 0

mcint
mcint

Reputation: 944

Absolutely. It sounds like you want to sanitize here as well, so "[0-9]{1,}%" or "[0-9]*.?[0-9]{1,}%" to ensure you match fractional percentages.

Normally I might have said, jfgi but this was too much (fun. EDIT)

http://www.regular-expressions.info/reference.html

You could do it with a something besides a regex but that would just be a loop that asserts that each character of input is a decimal digit, period, or percent sign, and that they are in the appropriate order.

Hope this helps!

Upvotes: 0

Related Questions