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!
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
| Component | Description | Matches |
|---|---|---|
^ | Start anchor | Ensures match from string start |
(100|[1-9]?\d(\.\d\d?)?) | Percentage value | Either 100 or 0-99 with optional decimals |
100 | Exact 100 | Literal 100 |
| | OR operator | Alternation |
[1-9]?\d | 0-99 integer | Optional 1-9, then any digit (0-99) |
(\.\d\d?)? | Optional decimal | Optional dot and 1-2 decimal digits |
$ | End anchor | Ensures 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:
0501000.550.2599.9912.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
- No percent sign: Doesn't include
%symbol (add%?if needed) - Fixed range: Hard-coded 0-100 range
- Decimal limit: Allows up to 2 decimal places
- Format only: Validates format, not actual percentage value
- 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!