我是JavaScript新手,正在尋找如何驗證我的表單按鈕單擊,但我的腳本不斷跳到第一個函數驗證名稱保持突出顯示,我不知道該怎麼去放在清除所有功能,所以它跳轉到下一個非驗證項目調用所有函數提交表格
function validate_form(form)
{
var complete=false;
if(complete)
{
clear_all();
complete = checkUsernameForLength(form.username.value);
}
if(complete)
{
clear_all();
complete = checkaddress(form.address.value);
}
if(complete)
{
clear_all();
complete = checkaddress(form.address.value);
}
if (complete)
{
clear_all();
complete = checkphone(form.phone.value);
}
if (complete)
{
clear_all();
complete = checkEmail(email.phone.value);
}
}
function clear_all()
{
document.getElementById('usernamehint').style.visibility= 'hidden';
document.basicform.username.style.backgroundColor='white';
document.getElementById("countryhint").style.visibility= 'hidden';
document.basicform.country.style.backgroundColor='white';
document.getElementById("").style.visibility= 'hidden';
document.basicformm.address.style.backgroundColor='white';
document.getElementById("").style.visibility= 'hidden';
document.basicform.phone.style.backgroundColor='white';
document.getElementById("").style.visibility= 'hidden';
document.basicform.email.style.backgroundColor='white';
}
function checkUsernameForLength(whatYouTyped)
{
var fieldset = whatYouTyped.parentNode;
var txt = whatYouTyped.value;
if (txt.length > 2) {
fieldset.className = "welldone";
return true;
} else {
fieldset.className = "";
return false;
}
}
function checkEmail(whatYouTyped)
{
var fieldset = whatYouTyped.parentNode;
var txt = whatYouTyped.value;
if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(txt)) {
fieldset.className = "welldone";
} else {
fieldset.className = "";
}
}
function checkaddress(whatYouTyped)
{
var fieldset = whatYouTyped.parentNode;
var txt = whatYouTyped.value;
if (txt.length > 3 && txt.length <10) {
fieldset.className = "welldone";
}
else {
fieldset.className = "";
}
}
function checkphone(whatYouTyped)
{
var fieldset = whatYouTyped.parentNode;
var txt = whatYouTyped.value;
if (/^((\+\d{1,3}(-|)?\(?\d\)?(-|)?\d{1,5})|(\(?\d{2,6}\)?))(-|)?(\d{3,4})(-|)?(\d{4})((x| ext)\d{1,5}){0,1}$/.test(txt)) {
fieldset.className = "welldone";
}
else
{
fieldset.className = "";
}
}
function addLoadEvent(func)
{
var oldonload = window.onload;
if (typeof window.onload != 'function')
{
window.onload = func;
}
else
{
window.onload = function()
{
oldonload();
func();
}
}
}
function prepareInputsForHints()
{
var inputs = document.getElementsByTagName("input");
for (var i=0; i<inputs.length; i++)
{
inputs[i].onfocus = function()
{
this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
}
inputs[i].onblur = function()
{
this.parentNode.getElementsByTagName("span")[0].style.display = "none";
}
}
}
強烈建議使用JQuery驗證:http://docs.jquery.com/Plugins/validation – Nix 2011-04-16 15:21:21
'document.getElementById(「」)'爲什麼空字符串?你是什麼意思?您無法讀取/寫入返回'null'的樣式 – atlavis 2011-04-16 15:25:22
'未捕獲的TypeError:無法讀取null屬性'樣式' - 這就是瀏覽器在執行'clear_all();' – atlavis 2011-04-16 15:32:04