2013-05-30 112 views
0

我有一些驗證代碼來驗證某些表單條目,我嘗試使用onsubmit命令調用它們,但是當我使用表單時,它似乎只有在行中調用的第一個函數工作,並且一旦成功驗證其餘的功能被忽略,表單剛剛提交?這裏是我的代碼:表單提交問題?

<script> 
function validateFamily() 
{ 
var family=document.getElementById('family'); 
if (family.value=="") 
    { 
    alert("Family name must be filled out"); 
    return false; 
    } 
else if (document.getElementById('family').value.length > 35) 
    { 
     alert("Family name cannot be more than 35 characters"); 
     return false; 
    } 
} 

function validateGiven() 
{ 
var given=document.getElementById('given'); 
if (given.value=="") 
    { 
    alert("Given name must be filled out"); 
    return false; 
    } 
else if (document.getElementById('given').value.length > 35) 
    { 
     alert("Given name cannot be more than 35 characters"); 
     return false; 
    } 
} 

function validDate() 
{ 
var chkdate = document.getElementById("dob").value 
if(document.getElementById("dob").value == "") 
{ 
    alert("Please enter Date of Birth") 
    return false; 
} 
else if(!chkdate.match(/^(0[1-9]|[12][0-9]|3[01])[\- \/.](?:(0[1-9]|1[012])[\- \/.](200)[0-4]{1})$/)) 
    { 
     alert('The entered date is an incorrect format'); 
    return false; 
    } 
} 

function validateMaleFemale() 
{ 
var radios = document.getElementsByName('malefemale'); 

for (var i = 0; i < radios.length; i++) 
{ 
    if (radios[i].checked) 
{ 
    return true; 
} 
}; 
alert ('You must choose male or female'); 
return false; 
} 

function validateAddress() 
{ 
var address = document.getElementById('address'); 
var stringa = document.getElementById('address').value; 
if (address.value=="") 
    { 
    alert("Address must be filled out"); 
    return false; 
    } 
else if (document.getElementById('address').value.length > 150) 
    { 
     alert("Address cannot be more than 150 characters"); 
     return false; 
    } 
else if (/[^a-zA-Z0-9\-\/]/.test(stringa)) 
    { 
     alert("Address can only contain alphanumeric, hypehns(-) and backslash's(/)") 
     return false; 
    } 
} 

function validateParent() 
{ 
var parent = document.getElementById('parenta'); 
var stringb = document.getElementById('parenta').value; 
if (parent.value=="") 
    { 
    alert("Parent/Carer name must be filled out"); 
    return false; 
    } 
else if (document.getElementById('parenta').value.length > 60) 
    { 
     alert("Parent/Carer name can not be more than 60 characters"); 
     return false; 
    } 
else if (/[^a-zA-Z\-\/]/.test(stringb)) 
    { 
     alert("Parent/Carer can only contain alphabetic and hypehns(-)") 
     return false; 
    } 
} 

function validateWork() 
{ 
var num1 = document.getElementById('workno').value; 

if (num1 !== "" && !num1.match(/\(\d{2}\)\d{8}/)) 
{ 
alert('That is not correct telephone number format'); 
return false; 
} 
} 

function validateHome() 
{ 
var num2 = document.getElementById('homeno').value; 

if (num2 !== "" && !num2.match(/\(\d{2}\)\d{8}/)) 
{ 
alert('That is not correct telephone number format'); 
return false; 
} 
} 

function validateMob() 
{ 
var num3 = document.getElementById('mob').value; 

if(num3 == "") 
{ 
alert('Please enter a mobile number'); 
return false; 
} 
else if (num3 !== "" && !num3.match(/\(\d{3}\)\d{8}/)) 
{ 
alert('That is not correct mobile number format'); 
return false; 
} 
} 
</script> 

<form name="eoiform" method="POST" action="<?php echo $_SERVER["PHP_SELF"];?>" id="eoi" onsubmit="return !!(validateFamily() && validDate() && validateAddress() && validateParent() && validateMaleFemale() && validateMob() && validateHome() && validateWork());"> 

因此,例如功能validateFamily()將正常工作,如果該字段爲空,但事情已經進入到後場的其他功能將被忽略,表單提交提醒我。任何人都可以請告訴我爲什麼發生這種情況或如何解決它?

+1

你需要顯示你的驗證碼 – CodingIntrigue

+0

爲什麼你需要'!!'後返回? –

+0

preventDefault應該做的竅門,確保你也返回false。 –

回答

1

編輯:你的一些驗證方法,默認情況下不返回一個值(即)時,驗證通過:

例:

function validateFamily() 
    { 
     var family=document.getElementById('family'); 
     if (family.value=="") 
     { 
      alert("Family name must be filled out"); 
      return false; 
     } 
     else if (document.getElementById('family').value.length > 35) 
     { 
      alert("Family name cannot be more than 35 characters"); 
      return false; 
     } 
     return true;// Add this to all validate Methods! 
    } 

在所有添加默認返回值validateXxx()方法。讓我知道這個是否奏效。

+0

這是否仍然允許個別功能警報?這樣用戶就可以確切地知道錯誤在哪裏? – AJJ

+0

我編輯了我的答案,如上所述,當方法被調用時,這將會逐個彈出個別提醒。 – ManKum

+0

我添加了返回真正的所有我的功能,並做了這個伎倆,非常感謝! – AJJ