Solved

How to find control in asp.net javascript client custom validator

Posted on 2008-10-14
1
1,849 Views
Last Modified: 2012-06-27
Goal is to validate on the client side (javascript) that a listbox has either no selections or at least a minimum number of rows selected.
I can sneak in the minimum number of rows, but need assistance finding the listbox, given the source or args parameters.


<script type="text/javascript" >

  function cvAnswer_7_2_4MinLengthListBox_ClientValidate(source, args) {

    alert(source.id);

    alert(args.ControlToValidate);

    alert(source.ControlToValidate);

    alert('zero');

    var regex = /^\D*\d+_(\d+)_/;

    var result = source.id.match(regex);

    var minLength = parseInt(result[1], 10);

    var lbx = document.getElementById(source.id);

    alert(lbx);

    alert(lbx.options);

    alert(lbx.options.length);

    var selectedCount = 0;

    alert('two');

    var i;

    alert('three');

    for (i = 0; i < lbx.options.length; i++) {

      if (lbx.options[i].selected) {

        selectedCount++;

      }

    }

    alert('four');

    args.IsValid = (selectedCount >= minLength);

    alert('five');

    alert(args.IsValid)

  }

</script>

Open in new window

0
Comment
Question by:brotherbill999
1 Comment
 

Accepted Solution

by:
brotherbill999 earned 0 total points
ID: 22720587
To find listBox, use: source.controltovalidate
Note that on Server side, it is ControlToValidate, but on javascript side it is all lowercase.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now