在提交表格之前如何检查其他两个不同的功能是否属实

时间:2022-11-24 14:36:37

My form submit code is below:

我的表单提交代码如下:

function test_final(){
  //condition 1 & condition 2 are return true then only form submitted true otherwise return false
  test_one();
  test_two();
  }


function test_one{ //condition 1;
  var one = document.getElementById('name').value;
  if(one == "") {return false;}
  else{return true;}
  }
function test_two{//condition 2;
  var two = document.getElementById('email').value;
  if(two == "") {return false;}
  else{return true;}
  }
<form onsubmit="return test_final()" action="test.php">
  <input type="text" id="name">
  <input type="text" id="email">
  <button>submit</button>

In my above code check both two condition are satisfied then only form submitted otherwise return false.but in this case its not working ..please correct my code or guide me on right way.Any help are appreciate..!

在我的上面的代码检查两个条件都满足然后只提交表单否则返回false。但在这种情况下它不工作..请更正我的代码或指导我正确的方式。任何帮助是欣赏..!

1 个解决方案

#1


0  

Try this:

function test_final(){
  //condition 1 & condition 2 are return true then only form submitted true otherwise return false
  return test_one() && test_two();
  
}


function test_one(){ 

  var one = document.getElementById('name').value;

  if(one == "") {
     return false;
  }
  else{
     return true;
  }
}

function test_two(){  //condition 2;
  
  var two = document.getElementById('email').value;

  if(two == "") {
     return false;
  }
  else{
    return true;
  }
}
<form onsubmit="return test_final()" action="test.php">
  <input type="text" id="name">
  <input type="text" id="email">
  <button>submit</button>
  </form>

#1


0  

Try this:

function test_final(){
  //condition 1 & condition 2 are return true then only form submitted true otherwise return false
  return test_one() && test_two();
  
}


function test_one(){ 

  var one = document.getElementById('name').value;

  if(one == "") {
     return false;
  }
  else{
     return true;
  }
}

function test_two(){  //condition 2;
  
  var two = document.getElementById('email').value;

  if(two == "") {
     return false;
  }
  else{
    return true;
  }
}
<form onsubmit="return test_final()" action="test.php">
  <input type="text" id="name">
  <input type="text" id="email">
  <button>submit</button>
  </form>