Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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: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 18-Feb-13 17:13pm
Edited 18-Feb-13 17:16pm
_Amy48.4K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try this regular expression ^[1-9][0-9]*(\.[0-9]+)?|0+\.[0-9]*[1-9][0-9]*$.
 
<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:CompareValidator ID="CompareValidator1" runat="server" ValueToCompare="0" ControlToValidate="TextBox1"
ErrorMessage="Must enter positive integers" Operator="GreaterThan" Type="Integer"></asp:CompareValidator>
 

--Amit
  Permalink  
v2
Comments
Sergey Alexandrovich Kryukov at 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 at 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 at 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 at 19-Feb-13 0:48am
   
I really appreciate your words and guidance. :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
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
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi ! This solution work exactly pass ValidationExpression="[1-9]*". In this case it accept value which is greater then '0'.
  Permalink  
v2
Comments
Mas11 at 19-Feb-13 1:15am
   
Please pass ValidationExpression="[1-9]*" in your "Regular Expression Validator" property field.
aravindnass at 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!..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

  Permalink  

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

  Print Answers RSS
0 OriginalGriff 259
1 Sergey Alexandrovich Kryukov 182
2 Hard_Rockz 153
3 Richard MacCutchan 125
4 Maciej Los 104
0 OriginalGriff 5,374
1 Sergey Alexandrovich Kryukov 4,713
2 Peter Leow 2,944
3 DamithSL 2,465
4 Maciej Los 2,270


Advertise | Privacy | Mobile
Web04 | 2.8.140718.1 | Last Updated 20 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid