'regular expression not to allow zero
regular expression not to allow zero
it should allow 0.0000001 as value but should not allow to enter 0.
I need validator not javascript
Solution 1:[1]
I think all you need is this ^(?=.*[1-9])\d*\.?\d*$
But, you could get fancy and allow only a single leading zero if its before a decimal point.^(?=.*[1-9])(?:[1-9]\d*\.?|0?\.)\d*$
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="Input is not valid."
ValidationExpression="^(?=.*[1-9])(?:[1-9]\d*\.?|0?\.)\d*$"></asp:RegularExpressionValidator>
<asp:Button ID="Button1" runat="server" Text="Button" />
Solution 2:[2]
a regexp like that?
([1-9](\.[0-9]+)?)|(0\.[0-9]*[1-9])
looks like working ;-)
if you remove the braces it looks more understandable:
[1-9](\.[0-9]+)? | 0\.[0-9]*[1-9]
Solution 3:[3]
If you want to allow both Positive or Negative numbers but not 0:
^(-?.*[1-9])\d*\.?\d*$
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | |
Solution 3 | sptramp |