Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am using a regular expression validator ...I want to check if number is greater than zero ..if 0 means it will not enter ..here is my validator ..check it and suggest a solution..
ASP.NET
<asp:RegularExpressionValidator ID="revAvailablePeriod" runat="server" ErrorMessage="Must be greater than 0" ControlToValidate="txtQuantity" ValidationExpression="^[1-9][0-9]*$" SetFocusOnError="true" ValidationGroup="AddAssests" >


in this can't type 0 its oky...but I have to type 0.2, 0.25 ,0.12 etc. .(since it greater than zero right!...)..

I want to type decimal also that grater than zero. So please help..
Posted
Updated 18-Feb-13 17:16pm
v2

In this very case, using Regular Expression is a bad idea.

Do completely different thing. First of all, if you are using a text box, you can filter out input so the user could enter only numbers (and backspace, which is also considered a character, #8). This is easy to do with JavaScript, in your case. And when you need to use the value, perform validation. In a bit more general form, if will be something like this:
C#
static bool Validate(string input, uint validMininum, uint validMaximum, out uint value) {
    bool valid = uint.TryParse(input, out value);
    if (valid)
       valid = validMininum <= value && value <= validMaximum;
    return valid;
}


I assumed uint, because you did not want to consider negative values, but you can use any other numeric type, for example, ulong.

—SA
 
Share this answer
 
v2
Try this regular expression ^[1-9][0-9]*(\.[0-9]+)?|0+\.[0-9]*[1-9][0-9]*$.

ASP.NET
<asp:regularexpressionvalidator id="revAvailablePeriod" runat="server" errormessage="Must be greater than 0" controltovalidate="txtQuantity" validationexpression="^[1-9][0-9]*(\.[0-9]+)?|0+\.[0-9]*[1-9][0-9]*$" setfocusonerror="true" validationgroup="AddAssests" xmlns:asp="#unknown"></asp:regularexpressionvalidator>


Or you can use compareValidator for to do the same:
ASP.NET
<asp:CompareValidator ID="CompareValidator1" runat="server" ValueToCompare="0" ControlToValidate="TextBox1"
ErrorMessage="Must enter positive integers" Operator="GreaterThan" Type="Integer"></asp:CompareValidator>



--Amit
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 19-Feb-13 0:05am    
This is the case when using Regular Expressions is a bad idea. The perfect solution is using TryParse.
Besides, there are cases when your validation will be invalid. Example: if a valid range is 0 to uint.MaximumValue, it won't work, because supposed you allow correct maximum number of digits. In this case, when a user types all 9s, resulting value will meet your Regex criterion, but will be greater then uint.Maximum, and uint.Parse will fail.

Agree? Please see my answer.... (I did not vote.)
—SA
_Amy 19-Feb-13 0:32am    
Yes, totally. Since OP was looking for Regular Expression, I posted that. Anyway, thanks for the guidance.
Sergey Alexandrovich Kryukov 19-Feb-13 0:43am    
You see, I think this is a wrong idea to rely on OP's idea what to use to solve the problem. The focus should be on the problem.
And for OP, it's a wrong idea to contaminate the question with the ideas of how to solve the problem, as these preoccupation with such ideas can be misleading...

By the way, pay attention that your solution is formally incorrect, so, not only the idea of Regex is bad, the problem is not solvable just with Regex...
—SA
_Amy 19-Feb-13 0:48am    
I really appreciate your words and guidance. :)
 
Share this answer
 
Hi ! This solution work exactly pass ValidationExpression="[1-9]*". In this case it accept value which is greater then '0'.
 
Share this answer
 
v2
Comments
Mas11 19-Feb-13 1:15am    
Please pass ValidationExpression="[1-9]*" in your "Regular Expression Validator" property field.
aravindnass 19-Feb-13 2:07am    
thank you for answer..but when this is added I can't enter 0.2,0.3.. etc." it is Greater than zero...right!..
If you need help in Regular Expression, try this[^].expresso 3 (Free of charge!).
 
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