Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi, everyone I need a help in ASP>net C#, please. I have a TextBox which getting

ID: 3855950 • Letter: H

Question

Hi, everyone

I need a help in ASP>net C#, please. I have a TextBox which getting a date and i would like the format of the date that user enter must be exactly like MM.DD.YYYY like (01.02.2001), and the user must enter a correct format til going to next step,

is anybody can help me, please?

this is my asp.net code.

<asp:Label ID="lblDateFrom" runat="server" Font-Bold="True" Font-Names="Arial" Font-Size="8pt">Date From:&nbsp</asp:Label>
<asp:TextBox ID="txtYearFrom" runat="server" Width="180px" MaxLength="10" BackColor="#FFFFCC" BorderColor="#006600" BorderStyle="Groove" ForeColor="Black"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="txtYearFrom" ValidationExpression="^((0[1-9])|(1[0-2])).((0[1-9])|(1[0-9])|(2[0-9])|(3[0-1])).[0-9y]{4}$" Display="Dynamic" SetFocusOnError="true" ErrorMessage="invalid date">(INVALID FORMAT)! Format must be like (MM.DD.YYYY).</asp:RegularExpressionValidator>
<ajx:ValidatorCalloutExtender ID="vceDateFrom" runat="server" SkinID="s_vce" TargetControlID="rfvDateFrom" Enabled="True" />
<asp:RequiredFieldValidator ID="rfvDateFrom" runat="server" ControlToValidate="txtYearFrom" ValidationGroup="Fc" Display="None" ErrorMessage="<b>Required Field Missing!</b>" SetFocusOnError="true" />

best regards

Explanation / Answer

Solution 1

Solution 2

<asp:TextBox ID="txtbox1" runat="server" Text="11/11/2011" />
<asp:CustomValidator runat="server" ClientValidationFunction="ValidateDate" ControlToValidate="txtbox1"
ErrorMessage="Invalid Date." ValidationGroup="Group1" />
<br />
<br />
<asp:Button ID="Button2" Text="Validate" runat="server" ValidationGroup="Group1" />
<script type="text/javascript">
function ValidateDate(sender, args) {
var dateString = document.getElementById(sender.controltovalidate).value;
var regex = /((0[1-9])|(1[0-2])).((0[1-9])|(1[0-9])|(2[0-9])|(3[0-1])).[0-9y]{4}$/;
if (regex.test(dateString)) {
var parts = dateString.split("/");
var dt = new Date(parts[1] + "/" + parts[0] + "/" + parts[2]);
args.IsValid = (dt.getDate() == parts[0] && dt.getMonth() + 1 == parts[1] && dt.getFullYear() == parts[2]);
} else {
args.IsValid = false;
}
}
</script>

Hope your question has been resloved. Please do not forget to give a positive like to the answer. Thank you.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote