Rohan
Rohan

Reputation: 1

Multiple conditions not firing in javascript

i am having javascript function written to check if the dropdown value from an aspx page has value "Completed" or "Cancelled".if it is then check if the date and time is not null. But the function never fires. the code is below.

function EnableValidator() {
    var drp = document.getElementById('<%=drpcallstatus.ClientID %>');
    var txt = drp.options[drp.selectedIndex].text;
    var dt = document.getElementById('<%=txtcompletedate.ClientID %>');
    var ct = document.getElementById('<%=txtcomptime.ClientID %>');



    if ((txt == "Completed" | txt=="Cancelled") && (dt===null | ct===null))  {

            alert("Please Enter the Completed Date and Time");

            return false;

    }

The function is called from asp.net button

 <asp:Button ID="btnsubmit" runat="server" Text="Submit"  OnClientClick="return EnableValidator()"  onclick="btnsubmit_Click" />

Upvotes: 0

Views: 80

Answers (2)

Lix
Lix

Reputation: 47966

The problem here is that you are not using the correct operator for your OR.

if ((txt == "Completed" || txt=="Cancelled") && (dt===null || ct===null))  {
  ...
}

Use double pipe characters || for an OR operator in a conditional expression.

Upvotes: 1

Francois Borgies
Francois Borgies

Reputation: 2408

try :

if ((txt == "Completed" || txt=="Cancelled") && (dt===null || ct===null))  {
            alert("Please Enter the Completed Date and Time");
            return false;
}

You have to use || for it works.

Upvotes: 1

Related Questions