0

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

Tim Vavra
  • 537
  • 1
  • 19
  • 35
  • Sounds like you're looking for the `RequiredFieldValidator`, among others: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.requiredfieldvalidator.aspx – David Mar 20 '13 at 14:05
  • Have you heard about validation controls? http://msdn.microsoft.com/en-us/library/debza5t0(v=vs.100).aspx – Alexander Tsvetkov Mar 20 '13 at 14:06

5 Answers5

1

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

Blachshma
  • 17,097
  • 4
  • 58
  • 72
  • I also have to compare a drop down list to a login name. I am capturing the login name using windows authentication when the user selects the web form. I have a variable stored as @User. How do I make that work in a compare validator if I don't have a control? Do I have to put a control out there which is not visible? – Tim Vavra Mar 20 '13 at 20:10
  • Not sure I understand the exact scenario, but maybe a [HiddenField](http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hiddenfield.aspx) can help – Blachshma Mar 20 '13 at 20:12
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

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
0

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)

LuigiEdlCarno
  • 2,410
  • 2
  • 21
  • 37
0

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

Community
  • 1
  • 1
Joe Ratzer
  • 18,176
  • 3
  • 37
  • 51
0

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" />
Toon Casteele
  • 2,479
  • 15
  • 25