Trouble with JavaScript and creating a custom validator

Discussion in 'ASP .Net Web Controls' started by Nathan Sokalski, Apr 8, 2009.

  1. 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.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/
     
    Nathan Sokalski, Apr 8, 2009
    #1
    1. Advertising

  2. 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




    "Nathan Sokalski" <> wrote in message
    news:u9VfKp$...
    >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.
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
    >
     
    S. Justin Gengo, Apr 8, 2009
    #2
    1. Advertising

  3. Nathan Sokalski

    bruce barker Guest

    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 Sokalski wrote:
    > 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.
     
    bruce barker, Apr 8, 2009
    #3
  4. 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

    http://www.nathansokalski.com/

    "bruce barker" <> wrote in message
    news:...
    > 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 Sokalski wrote:
    >> 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.
     
    Nathan Sokalski, Apr 8, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    9,800
    ganniss
    Jan 19, 2006
  2. Replies:
    4
    Views:
    939
    Richter~9.6
    Feb 13, 2007
  3. Nathan Sokalski
    Replies:
    3
    Views:
    371
    Nathan Sokalski
    Apr 8, 2009
  4. Scott Yenor

    How to get custom control to work with custom validator

    Scott Yenor, Jan 27, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    227
    Scott Yenor
    Jan 27, 2004
  5. Assimalyst
    Replies:
    1
    Views:
    121
    Mick White
    Aug 11, 2005
Loading...

Share This Page