|
-
Jul 28th, 2004, 12:25 AM
#1
Thread Starter
New Member
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.
-
Jul 28th, 2004, 07:03 AM
#2
I wonder how many charact
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!
-
Jul 28th, 2004, 07:05 AM
#3
I wonder how many charact
Here's the VS generated code-behind:
VB Code:
Public Class WebForm1
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents message As System.Web.UI.WebControls.Label
Protected WithEvents first As System.Web.UI.WebControls.TextBox
Protected WithEvents last As System.Web.UI.WebControls.TextBox
Protected WithEvents birthdate As System.Web.UI.WebControls.TextBox
Protected WithEvents birthdateRFV As System.Web.UI.WebControls.RequiredFieldValidator
Protected WithEvents birthdateCV As System.Web.UI.WebControls.CompareValidator
Protected WithEvents birthdateREV As System.Web.UI.WebControls.RegularExpressionValidator
Protected WithEvents YearsBut As System.Web.UI.WebControls.Button
Protected WithEvents DaysBut As System.Web.UI.WebControls.Button
Protected WithEvents HyperLink1 As System.Web.UI.WebControls.HyperLink
Protected WithEvents ValidationSummary1 As System.Web.UI.WebControls.ValidationSummary
Protected WithEvents output As System.Web.UI.WebControls.Label
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
Sub Button_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DaysBut.Click, YearsBut.Click
End Sub
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|