Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 1.03 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Validating parts of a date/time that is entered as separate fields
  2. TextBox1.Text = "is a date value";
  3. TextBox2.Text = "is hour of a datetime";
  4. TextBox3.Text = "is minutes of a datetime";
  5.        
  6. protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
  7. {
  8.     DateTime dateOutput = default(DateTime);
  9.     DateTime.TryParse(TextBox1.Text, out dateOutput);
  10.  
  11.     if (dateOutput != DateTime.MinValue)
  12.     {
  13.         string dateString = string.Empty;
  14.  
  15.         if (TextBox2.Text == string.Empty && TextBox3.Text == string.Empty)
  16.             dateString = dateString = string.Format("{0} {1}:{2}", TextBox1.Text, DateTime.Now.Hour, DateTime.Now.Minute);
  17.         DateTime.TryParse(dateString, out dateOutput);
  18.     }
  19.     args.IsValid = (dateOutput != DateTime.MinValue);
  20. }
  21.        
  22. <script type="text/javascript">
  23.   function validateMyForm(){
  24.     //write code to validate user input.
  25.     if(!succeed)
  26.        return false;
  27.     return true;
  28.   }
  29. </script>
  30. <form id="form1" runat="server" onsubmit="return validateMyForm()">
  31.       ...
  32. </form>