Hello all
Any help would be appreciated. Just started using .NET and find myself caught in a loop. Seems that my validator is not working when I input a word rather than the required format of "MM/DD/YYYY". "MM/DD/YY" will give me the validator error message, but leaving it blank or typing in text will not. How can that be fixed? Code below:
Code:<script language="vb" runat="server"> Sub ButtonClick(sender As Object, E As EventArgs) dim bday As Date dim fname, lname as string fname = first.text lname= last.text Select Case sender.id Case = "YearsBut" bday = Convert.ToDateTime(birthdate.Text) Dim message As String = "According to my calculations: " & fname & " " & lname & " " & "is" & " " & DateDiff(DateInterval.Year, bday, now) birthdate.BackColor = System.Drawing.Color.LightYellow output.Text = message & " years old." Case = "DaysBut" bday = Convert.ToDateTime(birthdate.Text) Dim message As String = "According to my calculations: " & fname & " " & lname & " " & "is" & " " & DateDiff(DateInterval.Day, bday, now) birthdate.BackColor = System.Drawing.Color.LightBlue output.Text = message & " days old." End Select End Sub Sub Page_load() birthdateCV.ValueToCompare = DateTime.Now.ToString("MM/dd/yyyy") dim CurDate as DateTime = DateTime.Now message.Text = "No this page wasn't posted back" If IsPostBack then message.Text = "You requested this page at : " + CurDate End if End sub </script> <meta content="False" name="vs_showGrid"> <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR"> <meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE"> <meta content="JavaScript" name="vs_defaultClientScript"> <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema"> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <asp:label id="message" runat="server"></asp:label><br> <br> First name: <asp:textbox id="first" runat="server"></asp:textbox><br> <br> Last name: <asp:textbox id="last" runat="server"></asp:textbox> <br> <br> Date of Birth: <asp:textbox id="birthdate" runat="server"></asp:textbox> <asp:requiredfieldvalidator id="birthdateRFV" runat="server" ErrorMessage="Enter a Date of Birth" ControlToValidate="birthdate">*</asp:requiredfieldvalidator> <asp:comparevalidator id="birthdateCV" runat="server" ErrorMessage="The birthdate should be less than the current date" ControlToValidate="birthdate" Type="Date" Operator="LessThan">*</asp:comparevalidator> <asp:regularexpressionvalidator id="birthdateREV" runat="server" ErrorMessage="Please use this format: MM/DD/YYYY:" ControlToValidate="birthdate" ValidationExpression="^\d{1,2}\/\d{1,2}\/\d{4}$">*</asp:regularexpressionvalidator><br> <br> <asp:button id="YearsBut" OnClick="ButtonClick" runat="server" Text="Years"></asp:button><asp:button id="DaysBut" OnClick="ButtonClick" runat="server" Text="Days"></asp:button> <asp:HyperLink id="HyperLink1" runat="server" NavigateUrl="http://204.249.107.103/pdao/index.aspx" style="Z-INDEX: 102; LEFT: 151px; POSITION: absolute; TOP: 185px">Home</asp:HyperLink><br> <br> <asp:ValidationSummary id="ValidationSummary1" ShowMessageBox="True" runat="server" ForeColor="Blue" BorderStyle="Solid" BorderColor="Black" BorderWidth="1px" Width="402px" Height="36px" /> <asp:Label id="output" runat="server" style="Z-INDEX: 101; LEFT: 19px; POSITION: absolute; TOP: 301px" /> </form>




Reply With Quote