• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5674
  • Last Modified:

Date validation in ASP.NET 2.0

What is the best way to validate a date in ASP.NET 2.0? I have a TextBox control and want the date to be entered as yyyy/mm/dd and check that it is a valid date. I have been trying to use the AJAX MaskedEditExtender and MaskedEditValidator, but can't get this to work.

<asp:TextBox ID="tbDate" runat="server" />
<cc1:MaskedEditExtender ID="meeDate" runat="server" TargetControlID="tbDate" Mask="9999/99/99" MaskType="Date" CultureName="en:za" />
<cc1:MaskedEditValidator ID="mevDate" runat="server" ControlToValidate="tbDate" ControlExtender="meeDate" Text="Invalid date" Display="Dynamic"></cc1:MaskedEditValidator>

can anyone help or suggest a better method?
0
DerekWatling
Asked:
DerekWatling
  • 2
1 Solution
 
DhaestCommented:
0
 
ZvonkoSystems architectCommented:
Extend it to this:
<asp:TextBox ID="tbDate" runat="server" />
<cc1:MaskedEditExtender ID="meeDate" runat="server" TargetControlID="tbDate" Mask="9999/99/99" MaskType="Date" CultureName="en:za" />
<cc1:MaskedEditValidator ID="mevDate" runat="server" ControlToValidate="tbDate" ControlExtender="meeDate"  ClientValidationFunction="checkDate"
Text="Invalid date" Display="Dynamic"></cc1:MaskedEditValidator>

And add this function:
<script type="text/javascript">
function checkDate(source, clientside_arguments){        
  var checkDate = new Date(clientside_arguments.Value );
  clientside_arguments.IsValid=true;
  var dPart = clientside_arguments.Value.split("/");
  if(dPart[0]!=checkDate.getFullYear() ||
     dPart[1]!=checkDate.getMonth()+1 ||
     dPart[2]!=checkDate.getDate()){
       clientside_arguments.IsValid=false;
   }
}
</script>




0
 
DerekWatlingAuthor Commented:
The MaskedEditExtender/Validator were doing too many strange things. Have ended up writing my own JavaScript onfocus and onblur functions that do what I want.
0
 
ZvonkoSystems architectCommented:
You can ask here for question close: http:/Community_Support/General/newQuestionWizard.jsp
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now