2015-03-13 56 views
0

我试图阻止提交的表单,如果验证失败,但它目前不工作。 onclick方法用于隐藏输入,但其onsubmit方法失败。有任何想法吗?Javascript/PHP验证

 <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)等,但这个也失败了,亲切的问候

+0

用于前walidation使用f] [query.validate](https://github.com/DiegoLopesLima/Validate#readme),为PHP验证使用[尊重/验证](https://github.com/Respect/Validation) – walkingRed 2015-03-13 14:43:59

+0

请记住,黑客可以轻松绕过浏览器内验证。 – hanshenrik 2015-03-13 18:48:04

回答

3

<input>标签移动你的onsubmit="return validation(this)"属性到<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 
+0

简短而甜蜜的感谢,关于如何检查值是否是数组的一部分的任何想法? – Frog82 2015-03-13 14:48:18

+0

更新了我对Array问题的回答。 – wwwmarty 2015-03-13 18:18:10

+0

感谢您的帮助! – Frog82 2015-03-16 09:37:40