Trouble with JavaScript and creating a custom validator

  • Thread starter Nathan Sokalski
  • Start date
N

Nathan Sokalski

I am creating a validator by inheriting from the BaseValidator class, which
I have done many times before. I am working on creating the JavaScript to
add the client-side validation capability to the validator, which I know is
done by adding an evaluationfunction attribute, which I have also done
before. I have done this, but when validation occurs I receive a message
saying that the function does not exist. I did a view source, and both the
evaluationfunction attribute and JavaScript function are where they should
be. But the more confusing part is that if I remove the body from the
function (in other words, make a validation function that doesn't do
anything), than doesn't complain or give any errors. I even tried adding the
one variable declaration, and it didn't hurt anything. The first thing I
narrowed it down to that gave me this error is the following:


for(var opt in
document.getElementById(val.clientid).options[]){document.writeln(opt.text);}


As you can see, this is a <select> element (which in this case came from a
ListBox) and I am just displaying the text of all the items (my actual
function does more, but I figure I should just try to access a simple text
property before perfecting the rest of my code). Because adding this line is
what causes me to get the error saying my function doesn't exist, I was
wondering if anyone sees anything in this code (NOTE: val.clientid is an
attribute I added as part of my validator's code, val is the name I used as
the parameter name in my function). Any help would be appreciated. Thanks.
 
S

S. Justin Gengo

Nathan,

I have a checkboxlist requiredfieldvalidator control that I made using much
the same process that you describe. While a checkboxlist is very different
than a listbox the way you reference the items is very similar. You may want
to look at my source code. I can't promise it will help, but perhaps it
would steer you in the right direction.

(It's in VB.NET but the javascript is javascript)

The full source code is downloadable from here:
http://www.aboutfortunate.com/Component-Library.aspx

--
Sincerely,

S. Justin Gengo, MCP
Web Developer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
B

bruce barker

you have a couple bugs in this code.

in javascript the "for(var i in a)" the loop variable is set to the
index value, not the item in the collection. so you opt will be a number
not an option.

the val parameter is the validator, and it does not have a clientid
property. you probably want the controltovalidate property.

-- bruce (sqlwork.com)
 
N

Nathan Sokalski

First of all, as I said in my original post, I added the clientid as part of
my code in the AddAttributesToRender method as follows:

writer.AddAttribute("clientid", Me.GetControlRenderID(Me.ControlToValidate))

Here is the JavaScript I have now:

function MinMaxSelectedValidatorIsValid(val)
{
for(var opt in document.getElementById(val.clientid).options[])
{
document.writeln(document.getElementById(val.clientid).options[opt].text);
}
return true;
}

Which is giving me the error:

Error: 'MinMaxSelectedValidatorIsValid' is undefined

But if I do a View Source in my browser, I can see that the function is
there, exactly as I expected. Why is it telling me my function is undefined?
Thanks.
--
Nathan Sokalski
(e-mail address removed)
http://www.nathansokalski.com/

bruce barker said:
you have a couple bugs in this code.

in javascript the "for(var i in a)" the loop variable is set to the index
value, not the item in the collection. so you opt will be a number not an
option.

the val parameter is the validator, and it does not have a clientid
property. you probably want the controltovalidate property.

-- bruce (sqlwork.com)


Nathan said:
I am creating a validator by inheriting from the BaseValidator class,
which I have done many times before. I am working on creating the
JavaScript to add the client-side validation capability to the validator,
which I know is done by adding an evaluationfunction attribute, which I
have also done before. I have done this, but when validation occurs I
receive a message saying that the function does not exist. I did a view
source, and both the evaluationfunction attribute and JavaScript function
are where they should be. But the more confusing part is that if I remove
the body from the function (in other words, make a validation function
that doesn't do anything), than doesn't complain or give any errors. I
even tried adding the one variable declaration, and it didn't hurt
anything. The first thing I narrowed it down to that gave me this error
is the following:


for(var opt in
document.getElementById(val.clientid).options[]){document.writeln(opt.text);}


As you can see, this is a <select> element (which in this case came from
a ListBox) and I am just displaying the text of all the items (my actual
function does more, but I figure I should just try to access a simple
text property before perfecting the rest of my code). Because adding this
line is what causes me to get the error saying my function doesn't exist,
I was wondering if anyone sees anything in this code (NOTE: val.clientid
is an attribute I added as part of my validator's code, val is the name I
used as the parameter name in my function). Any help would be
appreciated. 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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top