Hi, I have an amount text field on my web form, it can not be zero or blank, how to validate it?
I used this compare validator: but it does not validate blank, any idea to add blank as well?
<asp:CompareValidatorID="CompareValidator1"runat="server"ValueToCompare="0"ControlToValidate="TextBoxAmount"ErrorMessage="Must enter positive integers"Operator="NotEqual"Type="Integer"></asp:CompareValidator>
<!-- Your Textbox -->
<asp:TextBox ID="YourTextBox" runat="server"/>
<!-- Your Required Field Validator that will ensure that a value is entered in YourTextBox -->
<asp:RequiredFieldValidator ID="RequiredFieldValidator"
ControlToValidate="YourTextBox"
Display="Static"
ErrorMessage="This field is required."
runat="server"/>
Peter Cong
Member
527 Points
681 Posts
How to validate amount field for not equal zero or blank?
Feb 21, 2013 12:41 AM|LINK
Hi, I have an amount text field on my web form, it can not be zero or blank, how to validate it?
I used this compare validator: but it does not validate blank, any idea to add blank as well?
<asp:CompareValidator ID="CompareValidator1" runat="server" ValueToCompare="0" ControlToValidate="TextBoxAmount" ErrorMessage="Must enter positive integers" Operator="NotEqual" Type="Integer"></asp:CompareValidator>
Rion William...
All-Star
27280 Points
4517 Posts
Re: How to validate amount field for not equal zero or blank?
Feb 21, 2013 01:36 AM|LINK
You'll likely want to use a RequiredFieldValidator instead.
<!-- Your Textbox --> <asp:TextBox ID="YourTextBox" runat="server"/> <!-- Your Required Field Validator that will ensure that a value is entered in YourTextBox --> <asp:RequiredFieldValidator ID="RequiredFieldValidator" ControlToValidate="YourTextBox" Display="Static" ErrorMessage="This field is required." runat="server"/>geniusvishal
Star
14214 Points
2803 Posts
Re: How to validate amount field for not equal zero or blank?
Feb 21, 2013 01:50 AM|LINK
Try this:
<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Validation Demo</title> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="TextBoxAmount" runat="server"></asp:TextBox> <br /> <asp:CompareValidator ID="CompareValidator1" runat="server" ValueToCompare="0" ControlToValidate="TextBoxAmount" ErrorMessage="Must enter positive integers" Operator="NotEqual" Type="Integer"></asp:CompareValidator> <br /> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Field Cant be left Blank." ControlToValidate="TextBoxAmount"></asp:RequiredFieldValidator> <br /> <br /> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /><br /> <br /> <asp:Label ID="Label1" runat="server" Font-Bold="True" ForeColor="#C00000"></asp:Label></div> </form> </body> </html>protected void Button1_Click(object sender, EventArgs e) { Label1.Text="You have Entered:"+TextBoxAmount.Text; }My Website
www.dotnetvishal.com