Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

client-side validation

Status
Not open for further replies.

henslecd

MIS
Apr 21, 2003
259
US
Is there a good way to validate a textbox entry by the user against a dataset?

I.e

Dataset: VENDORS - Cisco, Dell, Microsoft

I would like the string entered in the textbox (txtVendor) to be searched for in the dataset, and if found, thrown an error saying that the Vendor already exists.

I did something similiar with classic asp where i used a stored procedure to return a recordset, and if recordset was not equal to "", throw an error.

I thought that asp.net might have a better solution.

Thanks for the help.
 
Have a look at <asp:CustomValidator> This way you can set your own validation. However, I don't think there is an 'easy' way to do it.

Regards,


Lewis Harvey
lewis@lharvey.com
 
The CustomFieldValidator supports both client-side validation functions (through the ClientSideValidation property) and server-side validation functions (through the ServerValidate event).

Both the client validation routine and the ServerValidate event receive two arguments: an object that contains a reference to the validation and a ServerValidateEventArgs object that expose two properties: Value is the value in the input control to be validated, and IsValid is a property that you must set to True to accept the value or False to reject it.

Take a look a this JScript function which checks whether a number is even:
Code:
   <script language=&quot;JScript&quot;>
   <!--
   function EvenNumber(source, args)
   {
      var theNumber = args.Value;
      if (theNumber % 2 == 0)
         args.IsValue = true;
      else
         args.IsValid = false;
   }
   //-->

On the ServerValidate event, the code looks about the same:
Code:
   Public Sub CustVal_ServerValidate(ByVal source As Object, ByVal args As ServerValidateEventArgs) Handles CustVal.ServerValidate

      Dim theNumber As Integer = CInt(args.Value)
      If (theNumber Mod 2 = 0) Then
         args.IsValie = True
      Else
         args.IsValid = False
      End If
   End Sub

Finally, to enable the CusomValidator control, you must assign the ControlToValidate and the ClientValidateFunction properties:
Code:
   CustVal.ControlToValidate = &quot;txtNumber&quot;
   CustVal.ClientValidationFunction = &quot;EvenNumber&quot;
   CustVal.ErrorMessage = &quot;This field only accepts even numbers.&quot;

In your case, to implement the client function, you may use a ListBox control to add all acceptable values from the DataSet because the DataSet lives only on the server and you wouldn't be able to access it from client script. The ListBox, however, is rendered as <SELECT> in the browser and is accessible through script.

Note: I took the above example from the book &quot;Proramming Visual Basic .NET&quot;, Microsoft Press, by Francesco Balena, so that it would be easier to read. I intend no plagiarism!

JC


We don't see things as they are; we see them as we are. - Anais Nin
 
Thanks for your posts. I actually used the customvalidator with the onServerValidation event.

The function I called retrieved the dataset I needed, and then I compared the input (text, dropdown etc) values against that of the dataset.

'I used a for loop from 0 to recordcount of the dataset

from i = 0 to (recordcount) - 1

'then i compared

if value = dataset.Tables(0).Rows(i)(&quot;FIELDNAME&quot;).ToString()

then args.isvalid = true

else

args.invalid = false


I got similiar info to what jcruz wrote on 4guysfromrolla.com .
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top