所以我一直在嘗試做客戶端表單驗證。我試圖驗證名字,然後將其餘的工作。我假設Javascript應該檢查每個「onkeyup」的字段,並顯示我的消息,如果該字段中沒有字符,則需要「First Name Required」。我將在下面留下我的表單信息,然後是我以後使用的驗證代碼。我正在運行將'index.php'鏈接到'validation.js'的腳本。該腳本位於「」標記旁邊的index.php中。客戶端表單驗證。
感謝您的任何幫助,我真的很感激它。
<form method="POST" name="signup" action="php/processuserform.php">
<input id="firstname" onsubmit="validateFirstName()" placeholder="First Name"
type="text" /><label id="firstnameprompt"></label>
<br><br>
<input id="last_name" onkeyup="validateLastName()" placeholder="Last Name"
type="text" /><label id="last_nameprompt"></label>
<br><br>
<input id="email" onkeyup="validateEmail()" placeholder="Email" type="text" /><label
id="emailprompt"></label>
<br /><br />
<input id="password" onkeyup="validatePassword()" placeholder="Create Password"
type="password" /><label id="passwordprompt"></label>
<br /><br />
<strong>Male</strong><input id="male" value="male" type="radio" /><label
id="maleprompt"></label>
<strong>Female</strong><input id="female" value="female" type="radio" /><label
id="femaleprompt"></label>
<br /><br />
<label for="submit">"I Agree To <a href="#">Terms And Conditions"</a></label> <input
id="submit" value="Submit" type="submit" name="submit"/><br /><br />
======================================= ...
現在是我一直在嘗試使用的驗證。只是試圖確保字段不是空的。
function validateFirstName()
{
var name = document.getElementById("firstname").value;
if(name.length == 0)
{
producePrompt("First Name Required", "firstnameprompt", "red");
return false;
}
}
function producePrompt(message, promptlocation, color)
{
document.getElementById(promptLocation).innerHTML = message;
document.getElementById(promptLocation).style.color = color;
}
有什麼問題? – yogi
腳本標籤位於結束標籤的旁邊。 – user3066599
它不會顯示錯誤 – user3066599