Question

I have two text boxes, one for a scheduled date, and the other for a scheduled time. I want to pass validation if both text boxes are blank, or both have content. I want to fail validation if only one has content. All is working fine server side, and the following client-side code is working correctly in Chrome.

    function CheckScheduledDateTime(sender, args) {
        if (ctl00_MainContent_txtScheduledTime.value!="" || ctl00_MainContent_txtScheduledDate.value!="" )
        {
            if (ctl00_MainContent_txtScheduledTime.value!="" && ctl00_MainContent_txtScheduledDate.value=="")
            {
                args.IsValid=false;
                alert("Scheduled date is required");
            }
            else if (ctl00_MainContent_txtScheduledTime.value=="" && ctl00_MainContent_txtScheduledDate.value1!="")
            {
                args.IsValid=false;
                alert("Scheduled time is required");
            }
            else
            {
                args.IsValid=true;
            }

        }
        else 
        {
            args.IsValid = true;
        }         
    }

In Internet Explorer, it does not work, and I am getting the following error:

"Microsoft JScript runtime error: 'ctl00_MainContent_txtScheduledTime' is undefined"

The strange thing is, with it broken in Visual Studio, if I then try and step into into once more, it breaks again, but then if I try and step into it a third time it runs, and the validation works properly.

Is anybody able to shed some light on this?

Was it helpful?

Solution

You can use it like this

ctl00_MainContent_txtScheduledTime are not javascript variables until you initialize them using

var ctl00_MainContent_txtScheduledTime = document.getElementById('<%=txtScheduledTime.ClientID%>');

or you can use it like

(document.getElementById('<%=txtScheduledTime.ClientID%>').value!="" || document.getElementById('<%=txtScheduledDate.ClientID%>').value!="" )

Regards.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top