Back to Home

Percentage Validation Regex (0-100)

CronOS Team
regexpercentagevalidationtutorialnumber

Need to generate a regex pattern?

Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!

Generate Regex Pattern

Percentage Validation Regex (0-100)

Validate percentage values from 0 to 100 with optional decimal places using regex pattern.

Pattern Breakdown

regex
^(100|[1-9]?\d(\.\d\d?)?)$

Components

ComponentDescriptionMatches
^Start anchorEnsures match from string start
(100|[1-9]?\d(\.\d\d?)?)Percentage valueEither 100 or 0-99 with optional decimals
100Exact 100Literal 100
|OR operatorAlternation
[1-9]?\d0-99 integerOptional 1-9, then any digit (0-99)
(\.\d\d?)?Optional decimalOptional dot and 1-2 decimal digits
$End anchorEnsures match to string end

Detailed Breakdown

  • 100 - Exact 100 (no decimals)
  • [1-9]?\d - 0-99:
    • [1-9]? - Optional digit 1-9 (for 10-99)
    • \d - Any digit 0-9 (allows 0-9 and 10-99)
  • (\.\d\d?)? - Optional decimal part:
    • \. - Literal dot
    • \d\d? - One or two decimal digits

Examples

Valid:

  • 0
  • 50
  • 100
  • 0.5
  • 50.25
  • 99.99
  • 12.5

Invalid:

  • 101 (exceeds 100)
  • -5 (negative)
  • 100.5 (100 cannot have decimals)
  • 50.123 (too many decimal places)
  • abc (non-numeric)

Implementation

JavaScript

javascript
const percentageRegex = /^(100|[1-9]?\d(\.\d\d?)?)$/;
percentageRegex.test('50'); // true
percentageRegex.test('100'); // true
percentageRegex.test('50.25'); // true
percentageRegex.test('0.5'); // true
percentageRegex.test('101'); // false (exceeds 100)
percentageRegex.test('100.5'); // false (100 cannot have decimals)

Python

python
import re
percentage_regex = r'^(100|[1-9]?\d(\.\d\d?)?)$'
bool(re.match(percentage_regex, '50'))  # True
bool(re.match(percentage_regex, '100'))  # True
bool(re.match(percentage_regex, '50.25'))  # True
bool(re.match(percentage_regex, '101'))  # False (exceeds 100)

Go

go
percentageRegex := regexp.MustCompile(`^(100|[1-9]?\d(\.\d\d?)?)$`)
percentageRegex.MatchString("50") // true
percentageRegex.MatchString("100") // true
percentageRegex.MatchString("50.25") // true
percentageRegex.MatchString("101") // false (exceeds 100)

Limitations

  1. No percent sign: Doesn't include % symbol (add %? if needed)
  2. Fixed range: Hard-coded 0-100 range
  3. Decimal limit: Allows up to 2 decimal places
  4. Format only: Validates format, not actual percentage value
  5. No leading zeros: Accepts 00 (may want to prevent this)

When to Use

  • Percentage input validation
  • Progress indicators
  • Discount/commission calculations
  • When you need 0-100 range validation
  • Form input validation for percentages

For production, consider:

  • Adding percent sign support: ^(100|[1-9]?\d(\.\d\d?)?)%?$
  • Supporting different ranges if needed
  • Allowing more decimal places if required
  • Normalizing input (remove % sign, etc.)
  • Converting to decimal (divide by 100) for calculations

Need to generate a regex pattern?

Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!

Generate Regex Pattern