0

I have trouble with the nested if structure in javascript. Any help is appreciated.

function validateForm()
{
    var a = document.forms["demo1"]["addr1"].value;
    var b = document.forms["demo1"]["city"].value;
    //var c = document.forms["demo1"]["fname"].value;
    //var d = document.forms["demo1"]["lname"].value;
    //var f = document.forms["demo1"]["phno"].value;
    //var g = document.forms["demo1"]["email"].value;
    //var g1 = document.forms["demo1"]["cemail"].value;
    //var h = document.forms["demo1"]["pwd"].value;
    //var h1 = document.forms["demo1"]["cpwd"].value;
    if(b=="" || b==null)
    {
      alert("Please enter your city");
      return false;
      break;
    }
    else if(a=="" || a==null)
        {
            alert("Please enter your address");
            return false;
            break;
        }
    else {return true;} 
}

<form name = "demo1" action = "exp2.php" onsubmit = "return validateForm()" method = "POST">

The code works fine(as intended) if there is only one if statement. But I am not getting the intended result if more than one if else is been deployed.

Regards, Mani

2

4 Answers 4

5

First, you don't need break statements, they're useless in this context.

Second, you don't need to nest and, in fact, you shouldn't since checking of a and b is independent of each other:

if(b=="" || b==null)
{
    alert("Please enter your city");
    return false;
}

if(a=="" || a==null)
{
    alert("Please enter your address");
    return false;
}

return true;
Sign up to request clarification or add additional context in comments.

Comments

1

What about shorten ur code with reusable isEmpty function

function validateForm()
{

    var isEmpty = function ( name , label ){

          var val = document.forms["demo1"][ name ].value;

          if( ! val  )
          {
               alert("Please enter your "+ label );
               return true;     
          }

          return false;

    }         

    return !isEmpty( 'city', 'city') && 
           !isEmpty( 'addr1', 'address');

}

isEmpty return true or false

Comments

0
if(b=="" || b==null) {
alert("Please enter your city");
return false;
}
else if(a=="" || a==null) {
alert("Please enter your address");
return false;
}
else {
return true;
}

Comments

0

Like paxdiablo says you can use two separate if statements.

But if you only want to require an address when a city is entered you must realize that this is not a nested if statement. This is:

if(b=="" || b==null)
{
  alert("Please enter your city");
  return false;
}
else 
{
    if(a=="" || a==null)
    {
        alert("Please enter your address");
        return false;
    }
    else 
    {
        return true;
    }
}

A more reabable version would be, imo:

if(b=="" || b==null)
{
  alert("Please enter your city");
  return false;
}

if((b=="" || b == null) && (a=="" || a==null))
{
    alert("Please enter your address");
    return false;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.