我有一个表单,用户可以在其中生成系统的较低级别的帐户。我需要该表格来验证用户没有两次使用相同的用户名。我如何设置它,以便如果javascript返回true,表单将仅发布到下一页以发布到数据库?
<script type = "text/javascript">
function validate() {
if(document.myForm.namea == document.myForm.name2 || document.myForm.namea == document.myForm.name3 || document.myForm.namea == document.myForm.name4 || document.myForm.namea == document.myForm.name5 || document.myForm.namea == document.myForm.name6 || document.myForm.namea == document.myForm.name7 || document.myForm.namea == document.myForm.name8 || document.myForm.namea == document.myForm.name9 || document.myForm.namea == document.myForm.name10 || document.myForm.namea == document.myForm.name11 || document.myForm.namea == document.myForm.name12 || document.myForm.namea == document.myForm.name14 || document.myForm.namea == document.myForm.name15 || document.myForm.namea == document.myForm.name16 || document.myForm.namea == document.myForm.name17 || document.myForm.namea == document.myForm.name18 ){
alert("You have entered the same username twice");
document.myForm.namea.focus();
return false;
} else{
return ( true );
}
}
</script>
<form action='insertgenerate.php' method='POST' name="myForm" onsubmit="return(validate());" enctype='multipart/form-data'>
<button class="btn btn-sm btn-primary" type="submit" >Generate</button>
如果输入了两次用户名,则弹出警报,然后继续到下一页并发布到数据库,无论如何
答案 0 :(得分:0)
<script type = "text/javascript">
function validate() {
if(document.myForm.namea == document.myForm.name2 || document.myForm.namea == document.myForm.name3 || document.myForm.namea == document.myForm.name4 || document.myForm.namea == document.myForm.name5 || document.myForm.namea == document.myForm.name6 || document.myForm.namea == document.myForm.name7 || document.myForm.namea == document.myForm.name8 || document.myForm.namea == document.myForm.name9 || document.myForm.namea == document.myForm.name10 || document.myForm.namea == document.myForm.name11 || document.myForm.namea == document.myForm.name12 || document.myForm.namea == document.myForm.name14 || document.myForm.namea == document.myForm.name15 || document.myForm.namea == document.myForm.name16 || document.myForm.namea == document.myForm.name17 || document.myForm.namea == document.myForm.name18 ){
alert("You have entered the same username twice");
document.myForm.namea.focus();
return false;
} else{
return ( true );
}
}
function validator(){
if(validate() == true){
document.getElementById("myForm").submit();
}
</script>
<form action='insertgenerate.php' id="myForm" method='POST' name="myForm" enctype='multipart/form-data'>
<button class="btn btn-sm btn-primary" type="button" onclick="validator();" >Generate</button>