Tim Vavra
Tim Vavra

Reputation: 537

Error checking in a web form

Is there somewhere to see examples of error checking. I have a web form with several controls. Two of the controls are critical to updating a database and also for the reporting in a gridview.

I am not looking for extremely complex checking, just something to stop the user before the submitting an update to the database. For example, I have a calendar where the user needs to select a date. If they fail to select a date, I would like to not process the update and instead return a warning like "Are you nuts, enter a date!"

TIA

Upvotes: 0

Views: 74

Answers (5)

Joe Ratzer
Joe Ratzer

Reputation: 18549

Have a look at the CustomerValidator, for example:

<asp:CustomValidator runat="server"
    ID="DateRangeValidator" 
    ControlToValidate="DateTextBox"
    OnServerValidate="DateRangeValidator_Validate" 
    ErrorMessage="ERROR MESSAGE" />

Then implment DateRangeValidator_Validate on the server side, adding the relevant date validation logic.

You can also look at the RequiredFieldValidator, but that alone won't be enough because that won't stop users trying to enter dates that can't be inserted into, for example, SQL Server - 01/01/1066

Upvotes: 0

Blachshma
Blachshma

Reputation: 17385

Asp.net comes with a set of validation controls you can use to validate the input on the client side before passing it to the server.

In your case, you probably want a RequiredFieldValidator

For example:

<asp:TextBox runat="server" ID="tbInput" />
<asp:RequiredFieldValidator ControlToValidate="tbInput" ValidationGroup="A" ErrorMessage="This is a mandatory field" runat="server" id="RequiredFieldValidator11" />
<asp:Button runat="server" ValidationGroup="A" Text="Submit"/>

Note the usage of ValidationGroup and ControlToValidate properties

Upvotes: 1

Toon Casteele
Toon Casteele

Reputation: 2579

You're looking for the required field validator: https://web.archive.org/web/20181218053647/http://www.4guysfromrolla.com:80/webtech/090200-1.2.shtml Example:

<asp:RequiredFieldValidator runat="server"
  id="ValidatorName"
  ControlToValidate="ctrlToValidate"
  ErrorMessage="Message to display for invalid data..."
  display="Dynamic" />

Upvotes: 0

LuigiEdlCarno
LuigiEdlCarno

Reputation: 2415

Simply check the returned value from the control against 'nothing' or whatever the controls default value is.

Something like (pseudocode):

if(value is nothing) {
   // print 'Are you nuts? Select a date'
}else{
   // store it in the database

If you want more info, please show us some of your code. (edit your answer)

Upvotes: 0

All you need to check is

Required Field Validator Class (This is used when a form is submitted without filling out fields) http://msdn.microsoft.com/en-IN/library/system.web.ui.webcontrols.requiredfieldvalidator.aspx

Regular Expression Validator Class (This is used to check what you have entered is right, for ins, when a phone is provided under email field) http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.regularexpressionvalidator.aspx

Validation Summary Class (Bringing all the error statement messages from the above two classes to the screen. for ins, Email cannot be left empty , Phone number cannot contain alphabets, ...) http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.validationsummary.aspx

Upvotes: 0

Related Questions