Results 1 to 3 of 3

Thread: validation error????

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2004
    Posts
    4

    validation error????

    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>
    Last edited by pdao; Jul 28th, 2004 at 12:36 AM.

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    You seem to have all the necessary components...

    A requiredfieldvalidator to make sure the box is not left empty.

    A regularexpressionvalidator to make sure it follows a specific format

    A datecomparator to use a valid range.



    I ran it thru VS, and with the code you pasted, I get a compile error because you missed the ValueToCompare attribute in the comparatorvalidator.

    Other than that, it works flawlessly and as expected on my machine.

    Things to check:
    1) Don't try using Firefox or Mozilla or NEtscape or Opera when testing client-side validation, it only supports IE.

    2) If you are using VS, use the code-behind model!

  3. #3
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Here's the VS generated code-behind:
    VB Code:
    1. Public Class WebForm1
    2.     Inherits System.Web.UI.Page
    3.  
    4. #Region " Web Form Designer Generated Code "
    5.  
    6.     'This call is required by the Web Form Designer.
    7.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    8.  
    9.     End Sub
    10.     Protected WithEvents message As System.Web.UI.WebControls.Label
    11.     Protected WithEvents first As System.Web.UI.WebControls.TextBox
    12.     Protected WithEvents last As System.Web.UI.WebControls.TextBox
    13.     Protected WithEvents birthdate As System.Web.UI.WebControls.TextBox
    14.     Protected WithEvents birthdateRFV As System.Web.UI.WebControls.RequiredFieldValidator
    15.     Protected WithEvents birthdateCV As System.Web.UI.WebControls.CompareValidator
    16.     Protected WithEvents birthdateREV As System.Web.UI.WebControls.RegularExpressionValidator
    17.     Protected WithEvents YearsBut As System.Web.UI.WebControls.Button
    18.     Protected WithEvents DaysBut As System.Web.UI.WebControls.Button
    19.     Protected WithEvents HyperLink1 As System.Web.UI.WebControls.HyperLink
    20.     Protected WithEvents ValidationSummary1 As System.Web.UI.WebControls.ValidationSummary
    21.     Protected WithEvents output As System.Web.UI.WebControls.Label
    22.  
    23.     'NOTE: The following placeholder declaration is required by the Web Form Designer.
    24.     'Do not delete or move it.
    25.     Private designerPlaceholderDeclaration As System.Object
    26.  
    27.     Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
    28.         'CODEGEN: This method call is required by the Web Form Designer
    29.         'Do not modify it using the code editor.
    30.         InitializeComponent()
    31.     End Sub
    32.  
    33. #End Region
    34.  
    35.     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    36.         'Put user code to initialize the page here
    37.     End Sub
    38.  
    39.     Sub Button_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DaysBut.Click, YearsBut.Click
    40.  
    41.     End Sub
    42.  
    43.  
    44. End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width