Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
Hi all

I wrote this expression for validating % in textbox

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

Its working fine with input like 20.88%
but when i give 20.88 i.e. without %, that also it's taking..this i don't want

Please tell me what i am doing wrong..??
Posted

1 solution

Try:
"^100(\.0{0,2})? *%$|^\d{1,2}(\.\d{1,2})? *%$"

instead. I removed the '?'s right after the '%'s.

You should try to use Expresso[^] when you come to manipulate regular expressions.
 
Share this answer
 
v2
Comments
[no name] 24-Oct-13 6:13am    
Bingo...thanks phil..
phil.o 24-Oct-13 6:15am    
Beware, I updated the solution because I did not notice there where two parts in your regex. Both parts have to be corrected.
Brian A Stephens 24-Oct-13 9:59am    
Also, the expression allows "100.%", which is strange. The "0{0,2}" should be changed to "0[1,2}" to force at least one digit after the decimal.
phil.o 24-Oct-13 10:01am    
Good point ^^
Joezer BH 28-Oct-13 10:29am    
5קג!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900