Client Side DropDownList Validator problem

A

Assimalyst

Hi,


I'm attempting to write a piece of code that acts as a client side
validator for all drop down lists on a webform, giving an error if the
SelectedIndex = 0.


Here is what i have:


function clientValidateDDL(source, args)
{
var dropdown = document.getElementById(source­.controltovalidate);

if (dropdown.selectedindex == 0)
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}



}


However, it does not give an error for any selected index, including
index 0, so i'm presuming the var dropdown = ... line is not correctly

selecting the dropdownlist that launched the function.

ClientValidationFunction="clie­ntValidateDDL" is inserted in the
custom
validator tags. and ControlToValidate is also present and correct.


Any ideas?


Thanks.
 
S

Stephen Chalmers

Assimalyst said:
if (dropdown.selectedindex == 0)

should be:

if (dropdown.selectedIndex == 0)

Even if that's the only error, it's surprising your console didn't report anything...
 
M

Mick White

Assimalyst said:
Hi,


I'm attempting to write a piece of code that acts as a client side
validator for all drop down lists on a webform, giving an error if the
SelectedIndex = 0.


Here is what i have:


function clientValidateDDL(source, args)
{
var dropdown = document.getElementById(source­.controltovalidate);

if (dropdown.selectedindex == 0)
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}



}
Try this:
function checkSelects(form){
var f=form.getElementsByTagName('select'),ff=f.length;
while(f--){
if(!f[ff].selectedIndex) return false;
}
return true;
}

Not tested, though
Mick




[snip]
 
A

ASM

Assimalyst said:
Hi,


I'm attempting to write a piece of code that acts as a client side
validator for all drop down lists on a webform, giving an error if the
SelectedIndex = 0.

usualy :

function validateSelects(myForm) {
var ok = true;
var f = myform.elements;
for(var i=0;i<f.length;i++)
if(f.tagName.toLowerCase()=='select' && f.selectedIndex==0) {
ok = false;
alert('You didn\'t see this list :\n'+f.name);
f.focus();
return;
}
return ok;
}

used :
Here is what i have:


function clientValidateDDL(source, args)
{
var dropdown = document.getElementById(source­.controltovalidate)

not very clear : what is 'source­.controltovalidate' ?
as bellow you talk about 'ControlToValidate'
(sensitive case)
if (dropdown.selectedindex == 0)
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}



}


However, it does not give an error for any selected index, including
index 0, so i'm presuming the var dropdown = ... line is not correctly

selecting the dropdownlist that launched the function.

Is that the select widh launchs clientValidateDDL(source, args) ?

if yes (that's I understood) :
var dropdown = source;
and
<select onchange="clientValidateDDL(this,'myArgs');"
 
A

ASM

Mick said:
Try this:
function checkSelects(form){
var f=form.getElementsByTagName('select'),ff=f.length;

will not work with non-DOM browser (as my NC4.5)
on my idea, will not work with 'f' as counter for while()

function checkSelects(form){
var f='';
if(document.getElementById && document.getElementsByTagName){
f=form.getElementsByTagName('SELECT'),ff=f.length;
while(ff--)
if(!f[ff].selectedIndex||f[ff].selectedIndex==0) return false;
}
else {
var f=form.elements,ff=f.length;
while(ff--)
if(f[ff].tagName.toLowerCase()=='select')
if(!f[ff].selectedIndex||f[ff].selectedIndex==0) return false;
}
return true;
}
 
R

RobG

ASM said:
will not work with non-DOM browser (as my NC4.5)
on my idea, will not work with 'f' as counter for while()

function checkSelects(form){
var f='';
if(document.getElementById && document.getElementsByTagName){

No need to test getElementById, it isn't used.
f=form.getElementsByTagName('SELECT'),ff=f.length;
while(ff--)
if(!f[ff].selectedIndex||f[ff].selectedIndex==0) return false;
}
else {
var f=form.elements,ff=f.length;
while(ff--)
if(f[ff].tagName.toLowerCase()=='select')

You could have used 'nodeName' rather than 'tagName', does it make any
difference (I can't test Communicator 4.5)?
if(!f[ff].selectedIndex||f[ff].selectedIndex==0) return false;

Are there any browsers that allow a select to not have a value for
selectedIndex? I think only the first test is required, since if the
selected index is 0 or returns undefined it will evaluate to false:

if( !f[ff].selectedIndex ) return false;
}
return true;
}

Seems to me the 'else' part is all that's required. The initial 'if' is
more selective and so may be faster but I don't think the difference
will be significant (maybe the OP has lots of form controls but only a
few selects...).

function checkSelects( form ){
var f = form.elements, ff = f.length;
while( ff-- ){
if( f[ff].tagName.toLowerCase() == 'select' ){
if( !f[ff].selectedIndex ){
return false;
}
}
}
return true;
}
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top