2012-04-22 154 views
0

該複選框的驗證不起作用。它不會給出任何錯誤。你能幫我解決嗎?我怎樣才能在一個警報而不是一個一個地結合錯誤?javascript複選框驗證

感謝您的任何幫助。

HTML代碼:

<form class="contact_form" action="" method="post" name="contact_form" onsubmit="returnonFormSubmit(this)"> 
<li> 
<label for="First Name">First Name:</label> 
<input type="text" name="visitor_name" /><br /> 
</li> 

<li> 
<label for="condition">I agree with the terms and conditions.</label> 
<input type="checkbox" name="lan" /><br /> 
</li> 

<li> 
       <label for="Male">Male:</label> 
      <input type="radio" name="gender" value="m" /> &nbsp; Female:<input type="radio" name="gender" value="f" /><br /> 
       </li> 
       <li> 
       <label for="National Rating">National Rating:</label> 
       <select name="make"> 
       <option selected>-- SELECT --</option> 
       <option> Below 1200 </option> 
       <option> 1200 - 1500 </option> 
       <option> 1500 - 1800 </option> 
       <option> 1800 - 2100 </option> 
       <option> Above 2100 </option> 
       </select><br /> 
       </li> 

<li> 
<button class="submit" type="submit">Submit</button> 
</li> 
    <div id="error_message" style="color:#ff0000;"></div> 

javascript代碼:

function onFormSubmit(form_element) 
{ 
    var checked = 0; 
    var letters = /^[a-zA-Z]+$/; 

if (form_element.visitor_name.value.match(letters)) 
    { 
      true; 
      } 
    else 
      { 
    alert("Please enter a valid first name. For example; John."); 
    false; 
    } 
     if (form_element.lan.checked == false) 
      { 
      alert("Please accept the terms and conditions"); 
      false; 
      } 
if (form_element.gender[0].checked == false && form_element.gender[1].checked == false) 
    { 
    alert("Please select a gender."); 
    false; 
    } 
    if (form_element.make.selectedIndex == 0) 
      { 
      alert("Please select your rating interval."); 
      form_element.make.focus(); 
      false; 
      } 
    return true; 
} 
+0

你需要使用'返回X;'(不是簡單的'X;'),你有一個錯字上'的onsubmit = 「returnonFormSubmit(本)」' – ajax333221 2012-04-22 14:34:31

回答

1

您應該連接變量中的錯誤消息。

function onFormSubmit(form_element) 
{ 
    var checked = 0; 
    var letters = /^[a-zA-Z]+$/; 
    var errorMessage = ""; 
    if (!form_element.visitor_name.value.match(letters)) 
    { 
     errorMessage += "Please enter a valid first name. For example; John.\n"; 
    } 
    if (form_element.lan.checked == false) 
    { 
     errorMessage += "Please accept the terms and conditions\n"; 
    } 
    if (errorMessage != "") 
    { 
     alert(errorMessage); 
     return false;   
    } 
    return true; 
}​ 
1

你有onsubmit="returnonFormSubmit(this)"一個錯字。它應該是

onsubmit="return onFormSubmit(this)" 

在控制檯打開的情況下運行此操作會爲您提供有價值的錯誤/警告。試用Chrome的開發工具,Firefox的Firebug或類似軟件。

要將錯誤合併爲一個錯誤,您可以從空字符串msg = ''開始,如果出現錯誤,可將其附加到該錯誤。然後在函數底部alert(msg),如果非空則返回false,否則返回true。

+0

它的onsubmit =「返回onFormSubmit (這個)」。在這裏複製時,我犯了一個錯誤。謝謝。 – pepsicode 2012-04-22 14:20:13

+0

但它仍然沒有給出任何錯誤。 – pepsicode 2012-04-22 14:20:44

+0

開發者工具或Firebug說什麼?也許你的代碼中有另一個語法錯誤。 – Kleist 2012-04-22 15:16:59

1

修復了returnonFormSubmit(this)中的打印錯誤後,它可以在Chrome和Firefox中使用。

(順便說一句:你忘了回報)

要結合警報我會用一個數組。 例子:

function onFormSubmit(form_element) 
{ 
    var checked = 0; 
    var letters = /^[a-zA-Z]+$/; 

    var alerts = new Array(); 

    if (!form_element.visitor_name.value.match(letters)) 
     alerts.push("Please enter a valid first name. For example; John."); 

    if (!form_element.lan.checked) 
     alerts.push("Please accept the terms and conditions"); 

    if (alerts.length == 0) { 
     return true; 
    } 

    alert(alerts.join("\n")); 

    return false; 
} 
+0

我編輯了我的問題。你能檢查一下嗎?我感謝您的幫助。 – pepsicode 2012-04-22 14:45:27