Click here to Skip to main content
16,018,496 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi, i want to validate my text box for discount it must have one digit with % symbol or two digit with % symbol or "upto followed by one digit or two digit with % symbol"

for example:
5%,50%,upto5%,upto50%

how to i use regular expression for this in asp.net?
Posted
Comments
Please try something and come back with the Regular Expression, if it doesn't work.

Hi just want to mention something if you have such kind of input allow user to enter number and validate number with reqular expression and make % static SPAN control or lable
% also used querystring so sometime user can do sql injection
 
Share this answer
 
Hi vivekpazhani,
Here you go:
^(upto|)[0-9]{1,2}%$

^ - start of the string
$ - end of the string
(upto|) - either "upto" or nothing
[0-9]{1,2} - a digit or two digits ({1,2} means between 1 and 2 times)
% - match the char %


You can use it in C# like this:
C#
string input = MyTexBox.Text;
// Here we call Regex.Match.
Match match = Regex.Match(input, @"^(upto|)[0-9]{1,2}%$",
    RegexOptions.IgnoreCase);

// Here we check the Match instance.
if (match.Success)
{
   // do stuff
}
else
{
   // do some other stuff
}


Cheers,
Edo
 
Share this answer
 
v4
Your needed match pattern could be (pseudo code)
1) start with optional spaces
2) followed by optional text "upto" followed by one or more spaces
3) followed by either
3.1) one of the digits 0-9
3.2) or one of the digits 1-9 followed by one of the digits 0-9
4) followed by optional spaces an followed by "%"
5) end on optional spaces

Translated into C#:
C#
//1111222222222222333333333334444445555
@"^\s*(?:upto\s+)?[1-9]?[0-9]\s*[%]\s*$"


Use that in a Regex C# class[^].
Cheers
Andi
 
Share this answer
 

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