简体   繁体   中英

How do I add data validation to a web form field using ASP.NET?

This seems like I only need a line or two of code?

This is what I need to do:

  • Add data validation to the new phone field on the Contact Us form. We would only accept phones with the following format: 995-999-9999
  • Complete the contact us form so that you can submit it as an email. Send all your test emails to blah@boo.net. You can use any email account you have to send the email.

This needs programming APIs such as:

  • System.Net.Mail
    • MailMessage
    • SmtpClient
    • NetworkCredential

It seems easy but I am so very lost with it. Also this page does not show up when I run it. This is the page code: I hope I did it right, sorry if it is not correct. I am trying!

    <%@ Page Title="Contact Us" Language="vb" AutoEventWireup="false" MasterPageFile="~/Main.Master" CodeBehind="ContactUs.aspx.vb" Inherits="Week10.ContactUs" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<h1>Contact Us</h1>
<hr />

<table border="1">
    <tr>
        <td>From</td>
        <td>
            <asp:TextBox ID="txtFrom" runat="server" Width="400px"></asp:TextBox>
        </td>
        <td>
            <asp:RequiredFieldValidator ID="rfvFrom" CssClass="ErrMSG"  runat="server" ControlToValidate="txtFrom" ErrorMessage="*" ToolTip="From field is required"></asp:RequiredFieldValidator></td>
    </tr>
    <tr>
        <td>Subject</td>
        <td>
            <asp:TextBox ID="txtSubject" runat="server" Width="400px"></asp:TextBox>
        </td>
        <td><asp:RequiredFieldValidator ID="rfvSubject" CssClass="ErrMSG" runat="server" ControlToValidate="txtSubject" ErrorMessage="*" ToolTip="Subject field is required"></asp:RequiredFieldValidator></td>
    </tr>
    <tr>
        <td>Phone</td>
        <td>
            <asp:TextBox ID="txtPhone" runat="server" Width="100px"></asp:TextBox>
        </td>
         <td><asp:RequiredFieldValidator ID="rfvPhone" CssClass="ErrMSG"  runat="server" ControlToValidate="txtPhone" ErrorMessage="*" ToolTip="Phone field is required"></asp:RequiredFieldValidator>
            </td>
    </tr>
    <tr valign="top">
        <td>Comments</td>
        <td>
            <asp:TextBox ID="txtComments" runat="server" Rows="10" TextMode="MultiLine" 
                Width="400px"></asp:TextBox>
        </td>
        <td><asp:RequiredFieldValidator ID="rfvComments" CssClass="ErrMSG" runat="server" ControlToValidate="txtComments" ErrorMessage="*" ToolTip="Comments field is required"></asp:RequiredFieldValidator></td>
    </tr>
    <tr>
        <td colspan="3" align="center">
            <asp:Button ID="btnSubmit" runat="server" Text="Submit" /></td>
    </tr>
    <tr>
        <td colspan="3" align="center">
            <asp:Label ID="lblMSG" runat="server" Text=""></asp:Label>
        </td>
    </tr>
</table>
</asp:Content>   

Take a look at the RegularExpressionValidator.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.regularexpressionvalidator.aspx .

You can use that to specify an expression pattern for the phone number pattern you require.

For more info on regular expressions check out this tutorial: http://www.regular-expressions.info/tutorial.html

When building an expression it is easier to use a validator to make sure your pattern actually validates as you expect it to. You can find these all over the place on the web. here is one: http://tools.netshiftmedia.com/regexlibrary/#

And finally, here is a pretty simple expression to validate a number 995-999-9999. However, I don't think this is an accurate validation (phone numbers cannot start with one).

^\\d{2}[1-5]-\\d{3}-\\d{4}$

EDIT:

Add this after the required field validator for the phone:

<asp:RegularExpressionValidator ID="rgexPhone" CssClass="ErrMSG"  runat="server"     ControlToValidate="txtPhone" ErrorMessage="*" ToolTip="Phone field is invalid" ValidationExpression="^\d{2}[1-5]-\d{3}-\d{4}$" />

But you really should know what this is doing before blindly adding somebody else's code to your project.

Maybe this article will explain it a little better: http://msdn.microsoft.com/en-us/library/ff650303.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM