This tutorial shows how you can validate the input of users using ASP.NET and VB.This tutorial shows how the ASP.NET tools make it easy for you to validate user input, both client-side and server-side.
User Input Validation - Client & Server - in VB.NET
This tutorial shows how you can validate the input of users using ASP.NET and VB
This tutorial shows how the ASP.NET tools make it easy for you to validate user input, both client-side and server-side.
First
we add the form elements from the Toolbox, including the
RequiredFieldValidator, RegularExpressionValidator and the
ValidationSummary.
Note the CustomValidator control, which allows us
to create a custom rule for validation. This particular one checks to
see if a date is in the correct format:
|
<form id="form1" runat="server">
<div>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator1" runat="server"
ControlToValidate="textEmail" Display="Dynamic" ErrorMessage="Email
Address is Required"
ValidationGroup="AllValidators">*</asp:RequiredFieldValidator>
Email:
<asp:TextBox ID="textEmail" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator
ID="RegularExpressionValidator1" runat="server"
ControlToValidate="textEmail" Display="Dynamic" ErrorMessage="Email
Address must be in the format abc@xyz.abc"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
ValidationGroup="AllValidators">Invalid
Format</asp:RegularExpressionValidator><br />
<asp:RequiredFieldValidator
ID="RequiredFieldValidator2" runat="server"
ControlToValidate="textNumberInParty" ErrorMessage="Number in Party is
Required"
ValidationGroup="AllValidators">*</asp:RequiredFieldValidator>
Number in Party:
<asp:TextBox ID="textNumberInParty" runat="server" Width="45px"></asp:TextBox>
<asp:RangeValidator
ID="RangeValidator1" runat="server"
ControlToValidate="textNumberInParty" Display="Dynamic"
ErrorMessage="Please enter 1-20 for the number of people in your party"
MaximumValue="20" MinimumValue="1" Type="Integer"
ValidationGroup="AllValidators">Enter a number between 1 and
20</asp:RangeValidator><br />
<asp:RequiredFieldValidator
ID="RequiredFieldValidator3" runat="server"
ControlToValidate="textPreferredDate" Display="Dynamic"
ErrorMessage="Date is Required"
ValidationGroup="AllValidators">*</asp:RequiredFieldValidator>
Preferred Date:
<asp:TextBox ID="textPreferredDate" runat="server"></asp:TextBox>
<asp:CustomValidator
ID="CustomValidator1" runat="server"
ControlToValidate="textPreferredDate" Display="Dynamic"
ErrorMessage="Format: m/d/yyyy"
OnServerValidate="CustomValidator1_ServerValidate"
ValidationGroup="AllValidators"
ClientValidationFunction="validateDate">Invalid format
(m/d/yyyy)</asp:CustomValidator><br />
<asp:RequiredFieldValidator
ID="validatorRequiredPhoneNumber" runat="server"
ControlToValidate="textPhoneNumber" Display="Dynamic" ErrorMessage="You
must provide a phone number">*</asp:RequiredFieldValidator>
<asp:CheckBox
ID="checkPhoneConfirmation" runat="server" AutoPostBack="True"
OnCheckedChanged="checkPhoneConfirmation_CheckedChanged" Text="Confirm
Reservation by Phone" /><br />
Tel. No.
<asp:TextBox ID="textPhoneNumber" runat="server" Enabled="False"></asp:TextBox>
<asp:RegularExpressionValidator
ID="validatorRegExPhoneNumber" runat="server"
ControlToValidate="textPhoneNumber" Display="Dynamic"
ErrorMessage="Phone number format is invalid"
ValidationExpression="((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}">Invalid
Format</asp:RegularExpressionValidator><br />
<asp:Button
ID="buttonSubmit" runat="server" OnClick="buttonSubmit_Click"
Text="Submit" ValidationGroup="AllValidators" /><br />
<asp:Label ID="labelResponse" runat="server"></asp:Label><br />
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="AllValidators" />
</div>
</form> |
We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!
In the code-behind file we have the following:
|
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
labelResponse.Text = ""
End Sub
Protected Sub buttonSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)
If Page.IsValid Then
labelResponse.Text = "Your request has been processed. Thank you."
Else
labelResponse.Text = "Date is not valid."
End If
End Sub
Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As ServerValidateEventArgs)
Try
DateTime.ParseExact(args.Value, "d", Nothing)
args.IsValid = True
Catch
args.IsValid = False
End Try
End Sub
Protected Sub checkPhoneConfirmation_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
If checkPhoneConfirmation.Checked Then
textPhoneNumber.Enabled = True
validatorRequiredPhoneNumber.ValidationGroup = "AllValidators"
validatorRegExPhoneNumber.ValidationGroup = "AllValidators"
Else
textPhoneNumber.Enabled = False
validatorRequiredPhoneNumber.ValidationGroup = ""
validatorRegExPhoneNumber.ValidationGroup = ""
End If
End Sub
|
We migrated our web sites to Server Intellect
over one weekend and the setup was so smooth that we were up and
running right away. They assisted us with everything we needed to do
for all of our applications. With Server Intellect's help, we were able to avoid any headaches!
For client-side validation of the date field, we can use a bit of JavaScript:
Note: For this to work, the 'EnableClientScript' Property of the CustomValidator has to be set to True.
|
<script language="javascript">
function validateDate(oSrc, args)
{
var iDay, iMonth, iYear;
var arrValues;
arrValues = args.Value.split("/");
iMonth = arrValues[0];
iDay = arrValues[1];
iYear = arrValues[2];
var testDate = new Date(iYear, iMonth - 1, iDay);
if ((testDate.getDate() != iDay) ||
(testDate.getMonth() != iMonth - 1) ||
(testDate.getFullYear() != iYear))
{
args.IsValid = false;
return;
}
return true;
} </script>
|
|