1

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?

2
  • Heads-up, when using out-of the box validators you should be validating on the server-side also. For ints vs strings, use the RegularEspressionValidator. Look at the anti-xss library as well :) Commented Aug 17, 2011 at 22:23
  • Agreed, I wouldn't be relying on the asp:validators for everything. Please do a quick validation in the code behind for xss scripts and the likes Commented Aug 17, 2011 at 22:24

2 Answers 2

2

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" />
Sign up to request clarification or add additional context in comments.

5 Comments

there's no simple asp.net solution?
what about requiredfieldvalidator?
the RequiredFieldValidator on the asp:controls works fine but the jQuery plugin gives you a lot more control i feel. As for asp.net solution, you're using c# code for the backend validation so...
1

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

2 Comments

sent you a msg on the discussion

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.