简体   繁体   中英

asp.net: how to do data validation?

i am enabling the user using my asp.net application to add data into a sql server 2008 database.

what is the best way to insert data and be able to validate things like empty fields, integers vs strings?

i am currently using formview to insert the data. how do i validate user input?

Use a validation jQuery plugin for front end validation.

This is the one I use a lot. http://bassistance.de/jquery-plugins/jquery-plugin-validation/

Then in c# create a partial class of your model and use DataAnnotations to do some more in depth validation.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;

    namespace DataRepository
    {
        [MetadataType(typeof(Company_validation))]
        public partial class Company
        {
        }


        public class Company_validation
        {
            [Required]
            [DisplayName("Company name")]
            public string Name { get; set; }

            [Required]
            [DisplayName("Address")]
            public string AddressLine1 { get; set; }
            public string AddressLine2 { get; set; }

            [Required]
            public string Suburb { get; set; }

            [Required]
            public int Postcode { get; set; }

            [Required]
            public string State { get; set; }
        }

    }

So from here you can pretty much do any validation you would ever need.

Here are two examples of RequiredFieldValidators we used in a project once.

The RegularExpressions validator is the one you want to check against string vs int etc or to validate phone number, postcode or email;

<asp:RequiredFieldValidator ID="reqValTxtAppRef" runat="server" ErrorMessage="Please enter your application reference number or <a href='ChooseYourHealthCover.aspx'>click here</a> to begin a new application <br/>"
ControlToValidate="txtAppRef" Display="Dynamic" ValidationGroup="vgRetrieveApp"
SetFocusOnError="true" CssClass="error" Font-Bold="true" cau EnableClientScript="False" />

<asp:RegularExpressionValidator ID="regexpValTxtAppRef" runat="server" ControlToValidate="txtAppRef" ErrorMessage="This is not a valid application reference number. Please enter it again or <a href='ChooseYourHealthCover.aspx'>click here</a> to begin a new application <br>" ValidationExpression="(HQ|HA|hq|ha)\d{8}" ValidationGroup="vgRetrieveApp" Display="Dynamic" CssClass="error" Font-Bold="true" EnableClientScript="False" />

I'd get comfortable with asp.net validators first... After that, there are some really nice ones with jquery, etc.

Here is a great place to get an overview, as well as see them in action. It has a sandbox area, where you can test it out too. http://www.w3schools.com/aspnet/aspnet_refvalidationcontrols.asp

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