Conditional validation

Thread Solved

Join Date: Oct 2007
Posts: 13
Reputation: minbor is an unknown quantity at this point 
Solved Threads: 0
minbor minbor is offline Offline
Newbie Poster

Conditional validation

 
0
  #1
Oct 20th, 2009
Hi everyone,

I have a web page where a fields needs to be validated if another is not empty. e.g. 'date' needs to be checked if 'quantity' has a number in it. I’m not exactly a pro with .net and am fiddling with compare and custom validators. All help on how/if this can be done much appreciated
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 452
Reputation: Ramesh S will become famous soon enough Ramesh S will become famous soon enough 
Solved Threads: 82
Ramesh S Ramesh S is offline Offline
Posting Pro in Training
 
1
  #2
Oct 22nd, 2009
Try this code.

.aspx code
  1.  
  2. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="DemoPage32.aspx.cs" Inherits="DemoPage32" %>
  3.  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head runat="server">
  7. <title></title>
  8. </head>
  9. <body>
  10. <form id="form1" runat="server">
  11. <div>
  12. Quantity : &nbsp;&nbsp;&nbsp;
  13. <asp:TextBox ID="txtQuantity" runat="server"></asp:TextBox><br />
  14. Date : &nbsp;&nbsp;&nbsp;
  15. <asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
  16. <asp:CustomValidator ID="cvValidateDate" runat="server" ErrorMessage="Date must be entered"></asp:CustomValidator><br />
  17. <asp:Button ID="btnValidate" runat="server" Text="Validate"
  18. onclick="btnValidate_Click" />
  19.  
  20. </div>
  21. </form>
  22. </body>
  23. </html>

C# code
  1. protected void btnValidate_Click(object sender, EventArgs e)
  2. {
  3. if (!String.IsNullOrEmpty(txtQuantity.Text))
  4. {
  5. int qty = int.Parse(txtQuantity.Text);
  6. if (qty > 0)
  7. {
  8. if (String.IsNullOrEmpty(txtDate.Text))
  9. {
  10. cvValidateDate.ErrorMessage = "Date must be entered";
  11. cvValidateDate.IsValid = false;
  12. }
  13. }
  14. }
  15. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 13
Reputation: minbor is an unknown quantity at this point 
Solved Threads: 0
minbor minbor is offline Offline
Newbie Poster
 
0
  #3
Oct 22nd, 2009
Thanks Ramesh S, this is great. Exactly what I was looking for.

Just in case anyone is interested , here is the same script in VB:

  1.  
  2. Protected Sub btnValidate_Click(ByVal sender As Object, ByVal e As EventArgs)
  3. If Not [String].IsNullOrEmpty(txtQuantity.Text) Then
  4. Dim qty As Integer = Integer.Parse(txtQuantity.Text)
  5. If qty > 0 Then
  6. If [String].IsNullOrEmpty(txtDate.Text) Then
  7. cvValidateDate.ErrorMessage = "Date must be entered"
  8. cvValidateDate.IsValid = False
  9. End If
  10. End If
  11. End If
  12. End Sub
Reply With Quote Quick reply to this message  
Reply

Tags
asp.net, validation

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 759 | Replies: 2
Thread Tools Search this Thread



Tag cloud for asp.net, validation
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC