Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Client Validation (JavaScript) for CustomValidator Control

Reply
Thread Tools

Client Validation (JavaScript) for CustomValidator Control

 
 
avp
Guest
Posts: n/a
 
      12-16-2005
Hi,
We have an ASP.NET 2.0 (C#) application that has a web form with a
CheckBoxList control and a CustomValidator control. The CustomValidator
control is used to validate that at least one checkbox is checked in the
CheckBoxList control.

Everything works fine for the server-side validation. However, we're having
difficulties with writing a JavaScript function to perform the same
validation on the client-side.

Here's what we got:

<script type="text/javascript">
function validateChecked(oSrc, args)
{
args.IsValid = false;

var formToValidate = document.forms['aspnetForm'];

if (!formToValidate)
{
formToValidate = document.aspnetForm;
}

var controlIndex;
var numberOfControls = formToValidate.length;
var element;

for (controlIndex = 0; controlIndex < numberOfControls;
controlIndex++)
{
element = formToValidate[controlIndex];

if (element.type == "checkbox")
{
if (element.checked == true)
{
args.IsValid = true;
}
}
}
}
</script>

With this code, everything seems to work fine. The problem is that we have
other checkboxes on the form that are not part of the CheckBoxList control,
and this JavaScript code cannot distinguish between the checkboxes in the
CheckBoxList control that we want to validate and the other checkboxes that
we don't want to validate.

Does anyone know how to write this JavaScript code to validate that at least
one checkbox in our CheckBoxList control is checked, while ignoring any
other checkboxes on the web form?

Since ASP.NET assigns some funky IDs/Names when the CheckBoxList is rendered
(e.g.,
<input id="ctl00_ContentPlaceHolderMain_Daily1_CheckBoxLi stDaily_0"
type="checkbox"
name="ctl00$ContentPlaceHolderMain$Daily1$CheckBox ListDaily$0" /><label
for="ctl00_ContentPlaceHolderMain_Daily1_CheckBoxL istDaily_0">Sun</label>),
how can you reference the correct controls by ID/Name?

Is there a better way of doing this?

Thanks!


 
Reply With Quote
 
 
 
 
=?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
Guest
Posts: n/a
 
      12-16-2005
A CheckBoxList server control renders an HTML Table on the browser. If you
give an ID to that CheckBoxList, e.g.
<asp:CheckBoxList ID="chkList1" Runat=server >
<asp:ListItem Value="item1">Item 1</asp:ListItem>
<asp:ListItem Value="item2">Item 2</asp:ListItem>
</asp:CheckBoxList>

Then in Javascript you can access its checkboxes only by a script like this:

var chkList1= document.getElementById ("chkList1");
var arrayOfCheckBoxes= chkList1.getElementsByTagName("input");
for(var i=0;i<arrayOfCheckBoxes.length;i++)
//do as you wish with each arrayOfCheckBoxes[i] element
alert(arrayOfCheckBoxes[i].id); //would echo the control's ID

As for you second question on how to retrieve individual checkboxes by ID
(considering the lengthy ID syntax formulated by ASP.NET) I usually write a
Javascript function such as this:


function findControl(ControlName)
{ var ret=null;
var aControls = document.getElementsByTagName("input");
if (aControls)
{ for (var i=0; i< aControls.length ; i++)
{
if (aControls[i].id.lastIndexOf(ControlName) == aControls[i].id.length -
ControlName.length &&
aControls[i].id.length != ControlName.length &&
aControls[i].id.lastIndexOf(ControlName) > 0 )
{
ret =aControls[i];
break;
}
}
}
return ret;
}

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


"avp" wrote:

> Hi,
> We have an ASP.NET 2.0 (C#) application that has a web form with a
> CheckBoxList control and a CustomValidator control. The CustomValidator
> control is used to validate that at least one checkbox is checked in the
> CheckBoxList control.
>
> Everything works fine for the server-side validation. However, we're having
> difficulties with writing a JavaScript function to perform the same
> validation on the client-side.
>
> Here's what we got:
>
> <script type="text/javascript">
> function validateChecked(oSrc, args)
> {
> args.IsValid = false;
>
> var formToValidate = document.forms['aspnetForm'];
>
> if (!formToValidate)
> {
> formToValidate = document.aspnetForm;
> }
>
> var controlIndex;
> var numberOfControls = formToValidate.length;
> var element;
>
> for (controlIndex = 0; controlIndex < numberOfControls;
> controlIndex++)
> {
> element = formToValidate[controlIndex];
>
> if (element.type == "checkbox")
> {
> if (element.checked == true)
> {
> args.IsValid = true;
> }
> }
> }
> }
> </script>
>
> With this code, everything seems to work fine. The problem is that we have
> other checkboxes on the form that are not part of the CheckBoxList control,
> and this JavaScript code cannot distinguish between the checkboxes in the
> CheckBoxList control that we want to validate and the other checkboxes that
> we don't want to validate.
>
> Does anyone know how to write this JavaScript code to validate that at least
> one checkbox in our CheckBoxList control is checked, while ignoring any
> other checkboxes on the web form?
>
> Since ASP.NET assigns some funky IDs/Names when the CheckBoxList is rendered
> (e.g.,
> <input id="ctl00_ContentPlaceHolderMain_Daily1_CheckBoxLi stDaily_0"
> type="checkbox"
> name="ctl00$ContentPlaceHolderMain$Daily1$CheckBox ListDaily$0" /><label
> for="ctl00_ContentPlaceHolderMain_Daily1_CheckBoxL istDaily_0">Sun</label>),
> how can you reference the correct controls by ID/Name?
>
> Is there a better way of doing this?
>
> Thanks!
>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Modifying a customValidator error message from the client function? Damon ASP .Net 2 06-23-2009 02:45 PM
HOWTO: Dynamically Add CustomValidator Control To Validation Summary Dan Sikorsky ASP .Net 1 02-17-2005 07:34 PM
CustomValidator client-side scripting John Bonds ASP .Net 0 08-23-2004 03:33 PM
CustomValidator added at runtime --> server-side validation dropped peshrad ASP .Net Web Controls 1 07-22-2004 08:22 AM
Client script argument properties in CustomValidator ??? Jon Davis ASP .Net 1 01-21-2004 12:55 AM



Advertisments