Client-Side Validation with Validators and JavaScript

N

Nathan Sokalski

I am trying to learn a little more about how to add client-side validation
to custom validators that I write (by inheriting from the BaseValidator
class). I know that the name of the JavaScript function is assigned to an
attribute named 'evaluationfunction' and that the function returns true or
false. However, some of the things I don't know or are unclear about are:

1. What parameters does the function assigned to evaluationfunction receive?
If they are objects, what properties do they have?

2. On several of the websites I was looking at for help, they said the
function assigned to evaluationfunction had 2 parameters, but were unclear
on what they were.

3. One of the validators (or actually types of validators) I am working on
writing is validators that use client callbacks (also known as AJAX). I know
how to implement the ICallbackEventHandler, but since the callback must be
initiated by JavaScript, I am assuming the callback statement would be in
the function used for evaluationfunction. However, this would require
calling a function to apply the validation after returning from the
callback. From what I could get from the sites I could find, this would
involve calling the ValidatorUpdateDisplay function that exists in the
WebUIValidation.js file after returning. However, I was having a little
trouble figuring out what to pass it and where to get that value.

If anybody could help me here, it would be greatly appreciated. Thanks.
 
M

Manish

Hi Nathan,

Please refer to this link below for client side validation using the
CustomValidator control.

http://quickstarts.asp.net/QuickStartv20/aspnet/doc/validation/default.aspx

The CustomValidator server control calls a user-defined function to perform
validations that the standard validators can't handle. The custom function
can execute on the server or in client-side script, such as JScript or
VBScript. For client-side custom validation, the name of the custom function
must be identified in the ClientValidationFunction property. The custom
function must have the form function myvalidator(source, arguments). Note
that source is the client-side CustomValidator object, and arguments is an
object with two properties, Value and IsValid. The Value property is the
value to be validated and the IsValid property is a Boolean used to set the
return result of the validation.

Regards,
Manish

www.ComponentOne.com
 
N

Nathan Sokalski

First of all, I am not looking for information about the CustomValidator
control, I am inheriting the BaseValidator class, which does not have a
ClientValidationFunction property. I also need to know about the
ValidatorUpdateDisplay JavaScript function.
 
B

bruce barker

the basevalidator has no builtin support for calling clientfunctions, you
will need to write the hookup code yourself. client validation is pretty
simple. validators are rendered as a span. then javascript code is rendered
to create a javascript variable that references the span. then expando
properties are added to this variable. your valiadation code would add
additional properties via RegisterExpandoAttribute.

there are 7 expando properties the base validator renders:

enabled - bool used to enable/disable validation
controltovalidate - the ClientId of the control to validate
focusOnError - bool used to indicate set focus action
errormessage - message to display if not valid
display - display type
isvalid - results of validation (set by the caller)
validationGroup - the validation group

if you want a client function called, your code needs to add the expando
attribute:

evaluationfunction - name of the client function to cal

the evaluationfunction is called with one arg, the validator instance. your
client code can then look at any expando attributes to decided what action to
take. this fuction should return true/false for valid/not valid

the CustomValidator will render the evaluationfunction based on the
ClientValidationFunction server property. it will call the client function
with two args

clientFunction (val, args)

where val is the validaor instance, and args is an object with two properties

args.Value - the value of the control to validate
args.IsValid - always initialized to true

after the custom validator calls your client fucntion it returns
args.IsValid to its caller (the standard validation code), and does not use
your functions return value.

one advantage of the custom validator (besides hooking the client function)
is it has generic code to determine the value of the control to validate. if
you use the base, you will need to write this code.

pretty simple actually.

-- bruce (sqlwork.com)
 
N

Nathan Sokalski

That definitely helps clear up the difference between client validation for
CustomValidators and other validators, as well as what properties their
parameters have, it was a big help. However, in order to create the
validators I am attempting to make that use client callbacks, I believe I
need a little more information about ValidatorUpdateDisplay, or any other
steps I need to take or methods I need to call to update the validation
status without returning a value from the evaluationfunction. Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top