2013-06-24 56 views
0

嗨我已經設計了我的表單現在我不確定如何結合所有的警報消息,而不是他們一次彈出一個,請有人可以告訴我如何在簡單條款,因爲我是新的JavaScript。先謝謝你。下面是我的腳本。如何添加一個錯誤而不是多個錯誤

function validateForm() { 
// this part of the script will collate all errors into one should the user leave an input blank 
var Fname=document.forms["myForm"]["fname"].value; 
var Lname=document.forms["myForm"]["lname"].value; 
var address=document.forms["myForm"]["addr1"].value; 
var postcode=document.forms["myForm"]["pcode"].value; 
var email=document.forms["myForm"]["email"].value; 
var number=document.forms["myForm"]["tel"].value; 
var date=document.forms["myForm"]["mydate"].value; 
if (Fname==null || Fname=="" ||Lname==null || Lname=="" ||address==null || address=="" ||!postcode||!email||!number||(myForm.sex[0].checked == false) && (myForm.sex[1].checked == false)||(myForm.age[0].checked == false)&&(myForm.age[1].checked == false)&&(myForm.age[2].checked == false)&&(myForm.age[3].checked == false)&&(myForm.age[4].checked == false)||!date) 
    { 
    alert("Please make sure all fields are filled or checked correctly out "); 
    return false; 
    } 
    //end of collating script 
    //start of postcode script 
var regPostcode = /^[a-zA-Z]{1,2}\d[\dA-Za-z]? \d[a-zA-Z]{2}$/; 
if (!postcode.match(regPostcode)) 
{ 
    alert("That Post Code is incorrect, correct way mk4 4tr"); 
    return false; 
} 
//end of postcode script 
//start of email script 
var regEmail =/^\[email protected]\S+\.\S+$/; 

if (!email.match(regEmail)) 
{ 
    alert("That email is incorrect"); 
    return false; 
} 


// end of email script 
// start of phone number script 
var phonestring = /^(?:0|\+44)[12378]\d{8,9}$/; 
if (!number.match(phonestring)) 
{ 
    alert(" incorrect,correct format 01908234874"); 
    return false; 
} 
// end of phone script 
//start of gender script 

if ((myForm.sex[0].checked == false) && (myForm.sex[1].checked == false)) 
{ 
alert ("Please choose your Gender: Male or Female"); 
return false; 
} 
// end of gender script 
//start of age group script 
if((myForm.age[0].checked == false)&&(myForm.age[1].checked == false)&&(myForm.age[2].checked == false)&&(myForm.age[3].checked == false)&&(myForm.age[4].checked == false)){ 
alert("please select an age group"); 
return false; 
} 
// end of age script 
//start of datefield 
var dateformat=/^(?:(?:31\/(?:0[13578]|1[02])|(?:29|30)\/(?:0[13-9]|1[012])|(?:0[1-9]|1\d|2[0-8])\/(?:0[1-9]|1[0-2]))\/[2-9]\d{3}|29\/02\/(?:[2-9]\d(?:0[48]|[2468][048]|[13579][26])|(?:[2468][048]|[3579][26])00))$/; 
if (!date.match(dateformat)) 
{ 
    alert("format incorrect use dd/mm/yyyy make sure you are entering correct days to the month remember 30 days have september, april, june & november, only 28 days in february unless leap year next is 2016"); 
    return false; 
} 
var today = new Date(); 

var courseYear =date.substr(6,4) // use substr or substring to capture the last four digits 
var courseMonth =date.substr(3,2) // use substr or substring to capture the four and fifth digits 
var courseDay = date.substr(0,2)//e the first and second digits 

var dateToCompare = new Date(courseYear, courseMonth, courseDay); 

if (dateToCompare < today) { 
alert("this date is in the past"); 
return false; } 

//end of date field 
else 
{ alert(" Thank you a member of our team will get back to you shortly"); 

return true;} 
} 
+4

構建一個字符串,結尾處有一個字符串數組提醒它 – musefan

+0

存儲您所有的消息,而不是提醒他們的。驗證後,您可以使用它來顯示消息。 – Crisfole

+4

這就是第一個條件的一個地獄,如果塊... – crush

回答

1

所有的錯誤添加到一個數組,然後提醒他們在最後,如果有的話:

function validateForm() { 
    var errors = []; //array for holding errors 
    . 
    . 
    . 

    if (Fname==null || Fname=="" ||Lname==null || Lname=="" ||address==null || address=="" ||!postcode||!email||!number||(myForm.sex[0].checked == false) && (myForm.sex[1].checked == false)||(myForm.age[0].checked == false)&&(myForm.age[1].checked == false)&&(myForm.age[2].checked == false)&&(myForm.age[3].checked == false)&&(myForm.age[4].checked == false)||!date) { 
    errors.push("Please make sure all fields are filled or checked correctly out "); //add error 
    } 
    //end of collating script 
    //start of postcode script 
    var regPostcode = /^[a-zA-Z]{1,2}\d[\dA-Za-z]? \d[a-zA-Z]{2}$/; 
    if (!postcode.match(regPostcode)) { 
    errors.push("That Post Code is incorrect, correct way mk4 4tr"); //add error 
    } 
    //end of postcode script 
    //start of email script 
    var regEmail =/^\[email protected]\S+\.\S+$/; 

    if (!email.match(regEmail)) { 
    errors.push("That email is incorrect"); //add error 
    } 

    if(errors.length > 0) { 
    alert('The following errors occurred: ' + errors.join('\n')); //alert errors if they exist 
    return false; 
    } 
    return true; // allow submit 
} 
+0

nope。再試一次 – musefan

+0

需要刪除中間'return'語句。 –

+0

@BradChristie:謝謝你毀了我的樂趣! :( – musefan

2

創建一些集合類,你可以追加到,而是alert荷蘭國際集團independantly,只需將它們添加到集合中。喜歡的東西:

function validateForm(){ 
    var errors = []; // new array to hold all the errors 

    /* 
    validation code that instead of 
     alert('error') 
    use 
     errors.push('error'); 
    Also remove any premature `return` statements and 
    leave them until the end. 
    */ 

    // next check if there are errors 
    if (errors.length > 0){ 
    // display them 
    alert('Following errors found:\n- ' + errors.join('\n- ')); 

    // also return false to flag there was a problem 
    return false; 
    } 

    // if we reached this code there were no errors 
    return true; 
} 
相關問題