Validator code problem.

A

Assimalyst

Hi,

I have a code that returns invalid if a dropdownlist is at index 0.

function clientValidateCboBx(source, args)
{
var dropdown = document.getElementById(source.controltovalidate);
if(dropdown.selectedIndex == 0)
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}

As above it works fine, but I want it to only return invalid if the
control it's validating is enabled, if it's disabled it should always
be valid. I tried adding && dropdownlist.enabled == true to the if
statement, but then the validator always returns true even when the
selectedIndex == 0.

Any ideas on a solution?

Thanks
 
W

web.dev

Assimalyst said:
Hi,

I have a code that returns invalid if a dropdownlist is at index 0.

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

I don't see enough of your script to see what it is that you're passing
as a "source" argument. However, getElementById() method expects a
string argument, and like the name says an id.

For example:

<div id = "myId">...</div>

Then in your script:

var myDiv = document.getElementById("myId");
 
M

Mick White

Assimalyst said:
Hi,

I have a code that returns invalid if a dropdownlist is at index 0.

function clientValidateCboBx(source, args)
{
var dropdown = document.getElementById(source.controltovalidate);
if(dropdown.selectedIndex == 0)
{
args.IsValid = false;
}
else
{
args.IsValid = true;
}
}

As above it works fine, but I want it to only return invalid if the
control it's validating is enabled, if it's disabled it should always
be valid. I tried adding && dropdownlist.enabled == true to the if
statement, but then the validator always returns true even when the
selectedIndex == 0.

Any ideas on a solution?

args.IsValid = !dropdown.enabled && dropdown.selectedIndex;
Mick
 
R

Richard Cornford

Mick said:
Assimalyst wrote:

args.IsValid = !dropdown.enabled && dropdown.selectedIndex;

Typo? You know the pertinent property is called - disabled - and is true
when the control is disabled and false otherwise?

Richard.
 
M

Mick White

Richard said:
Mick White wrote:


Typo? You know the pertinent property is called - disabled - and is true
when the control is disabled and false otherwise?
Yes, I invented the property "enabled", but the logic is sound:


args.IsValid = dropdown.disabled && dropdown.selectedIndex;

Mick
 
L

Lee

Mick White said:
Yes, I invented the property "enabled", but the logic is sound:


args.IsValid = dropdown.disabled && dropdown.selectedIndex;

Almost:

args.IsValid = dropdown.disabled || dropdown.selectedIndex;
 

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,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top