Javascript/PHP Validation


Javascript/PHP Validation

如果验证失败,我试图阻止提交表单,但是目前它不起作用。onclick 方法用于隐藏输入,但失败的是 onsubmit 方法。有什么想法吗?

      <input type="submit" class="btn ccm-input-submit" id="register" name="register" value="Register" onsubmit="return validation(this)" onclick="copytextbox()"/>
      <?php echo $form->hidden('rcID', $rcID); ?>
<script>
function copytextbox()
{
    var textToCopy = document.getElementById('school').value;
    var whereToCopy = document.getElementById('<?php echo $school ?>');
    whereToCopy.value += textToCopy; 
}
function validation(f)
{
    var schoolText = document.getElementById('school').value;
    if (schoolText == '') { 
      alert('Please Choose a value from the dropdown box.');  
      return false;  // stop submission until textbox is not '' 
    }  else {
      f.submit();
      return false;
    }
}
</script>
另外,我

试图查看该值是否是数组的成员,我尝试使用indexOf(schoolText)等,但这也失败了,亲切的问候

onsubmit="return validation(this)"属性从 <input> 标记移动到 <form> 标记。

像这样测试数组:

// global array
var fruits = ["Banana", "Orange", "Apple", "Mango"];
function findFruit(val) {
  if(fruits.indexOf(val) == -1 {
    alert('not found');
  } else {
    alert('Found!')
  }
}
findFruit('Peach'); // not found