2017-09-26 91 views
0

コードを実行すると、JavaScriptでの検証が正しく機能していないため、正しい検証でフォームを送信しようとするたびにエラーメッセージが表示されます。動作していない部分は、郵便番号と状態if文です。これは検証をスキップして、エラーがあるという結論で終わります。これをどうやって解決するのですか?郵便番号と状態の組み合わせによるJavaScript検証

"use strict"; 
 
/*get variables from form and check rules*/ 
 
function validate() { 
 
    var errMsg = ""; /* stores the error message */ 
 
    var result = true; /* assumes no errors */ 
 
    var state = document.getElementById("state").value; 
 
    var postcode = document.getElementById("postcode").value; 
 
    var other = document.getElementById("other").checked; 
 
    //get varibles from form and check rules here 
 
    // if something is wrong set result = false, and concatenate error message 
 

 
    var dob = document.getElementById("dob").value.split("/"); 
 
    var date = new Date(dob[2], parseInt(dob[1]) - 1, dob[0]); 
 
    var today = new Date(); 
 
    var age = today.getFullYear() - date.getFullYear(); 
 

 
    if (age >= 80) { // Outside age ranges. 
 
    errMsg = errMsg + "You must be 80 or younger to apply for this job\n"; 
 
    result = false; 
 
    } 
 

 
    if (age <= 15) { // Outside age ranges. 
 
    errMsg = errMsg + "You must be 15 or older to apply for this job\n"; 
 
    result = false; 
 
    } 
 

 
    if (postcode.charAt(0) == 3 && state == "VIC") { 
 
    result = true; 
 
    } else if (postcode.charAt(0) == 8 && state == "VIC") { 
 
    result = true; 
 
    } else if (postcode.charAt(0) == 1 && state == "NSW") { 
 
    result = true; 
 
    } else if (postcode.charAt(0) == 2 && state == "NSW") { 
 
    result = true; 
 
    } else if (postcode.charAt(0) == 4 && state == "QLD") { 
 
    result = true; 
 
    } else if (postcode.charAt(0) == 9 && state == "QLD") { 
 
    result = true; 
 
    } else if (postcode.charAt(0) == 0 && state == "NT") { 
 
    result = true; 
 
    } else if (postcode.charAt(0) == 6 && state == "WA") { 
 
    result = true; 
 
    } else if (postcode.charAt(0) == 5 && state == "SA") { 
 
    result = true; 
 
    } else if (postcode.charAt(0) == 7 && state == "TAS") { 
 
    result = true; 
 
    } else if (postcode.charAt(0) == 0 && state == "ACT") { 
 
    result = true; 
 
    } else { 
 
    errMsg = errMsg + "State and postcode do not match\n"; 
 
    result = false; 
 
    } 
 

 
    if (other) { 
 
    errMsg = errMsg + "You have selected other skills, you must enter one other skill in the text box\n"; 
 
    result = false; 
 
    } 
 

 
    if (errMsg != "") { //only display message box if there is something to show 
 
    alert(errMsg); 
 
    } 
 
    return result; //if false the information will not be sent to the server 
 
} 
 

 
function init() { 
 

 
    var regForm = document.getElementById("regform"); // get ref to the HTML element 
 

 
    regForm.onsubmit = validate; //register the event listener 
 
} 
 

 
window.onload = init;
<!DOCTYPE html> 
 
<html lang="en"> 
 

 
<head> 
 
    <meta charset="utf-8" /> 
 
    <meta name="description" content="Prototype Website" /> 
 
    <meta name="keywords" content="HTML, CSS, JavaScript" /> 
 
    <title>The Virtual World</title> 
 
    <link href="styles/style.css" rel="stylesheet" /> 
 
    <script src="apply.js"></script> 
 
</head> 
 

 
<body> 
 
    <article> 
 
    <header> 
 
     <h1>The Virtual World</h1> 
 

 
     <nav> 
 
     <p class="menu"><a href="index.html#home">Home</a></p> 
 
     <p class="menu"><a href="jobs.html#jobs">Jobs</a></p> 
 
     <p class="menu"><a href="apply.html#apply">Apply</a></p> 
 
     <p class="menu"><a href="about.html#about">About</a></p> 
 
     <p class="menu"><a href="enhancements.html#enhancements">Enhancements</a></p> 
 
     </nav> 
 
    </header> 
 
    <section id="required"> 
 
     <h5>All fields with * are required</h5> 
 
    </section> 
 
    <form id="regform" method="post" action="http://mercury.swin.edu.au/it000000/formtest.php"> 
 
     <fieldset> 
 
     <legend>Job Application</legend> 
 
     <p><label for="JobID">*Job ID:</label> 
 
      <input type="text" name="JobID" id="JobID" maxlength="5" size="10" pattern="^[a-zA-Z]{2}[0-9]{3}$" required="required" /></p> 
 
     <section id="choose"> 
 
      <h5>Please choose from QM593 or CS197</h5> 
 
     </section> 
 
     <fieldset> 
 
      <legend>Personal Details</legend> 
 
      <p><label for="title">*Title:</label> 
 
      <select name="title" id="title" required="required"> 
 
      <option value="">Please Select</option>   
 
      <option value="title">Dr</option> 
 
      <option value="title">Mr</option> 
 
      <option value="title">Miss</option> 
 
      <option value="title">Mrs</option> 
 
      <option value="title">Ms</option> 
 
     \t </select></p> 
 

 
      <p><label for="firstName">*First Name:</label> 
 
      <input type="text" name="firstName" id="firstName" maxlength="20" size="20" pattern="^[a-zA-Z ]+$" required="required" /> 
 

 
      <label for="familyName">*Family Name:</label> 
 
      <input type="text" name="familyName" id="familyName" maxlength="20" size="20" pattern="^[a-zA-Z ]+$" required="required" /></p> 
 

 
      <p><label for="midName">Middle Name:</label> 
 
      <input type="text" name="midName" id="midName" maxlength="20" size="20" pattern="^[a-zA-Z ]+$" /></p> 
 

 
      <p><label for="dob">*Date of Birth:</label> 
 
      <input type="text" name="dob" id="dob" placeholder="dd/mm/yyyy" pattern="\d{1,2}/\d{1,2}/\d{4}" maxlength="10" size="10" required="required" /></p> 
 

 
      <p>*Gender: 
 
      <label for="male">Male</label> 
 
      <input type="radio" id="male" name="sex" value="male" required="required" /> 
 
      <label for="female">Female</label> 
 
      <input type="radio" id="female" name="sex" value="female" required="required" /></p> 
 

 
      <p><label for="street">*Street Address:</label> 
 
      <input type="text" name="street" id="street" maxlength="40" size="30" required="required" /></p> 
 

 
      <p><label for="suburb">*Suburb/town:</label> 
 
      <input type="text" name="suburb" id="suburb" maxlength="40" size="20" required="required" /></p> 
 

 
      <p><label for="state">*State:</label> 
 
      <select name="state" id="state" required="required"> 
 
      <option value="">Please Select</option>   
 
      <option value="state">VIC</option> 
 
      <option value="state">NSW</option> 
 
      <option value="state">QLD</option> 
 
      <option value="state">NT</option> 
 
      <option value="state">WA</option> 
 
      <option value="state">SA</option> 
 
      <option value="state">TAS</option> 
 
      <option value="state">ACT</option> 
 
     \t </select></p> 
 

 
      <p><label for="postcode">*Postcode:</label> 
 
      <input type="text" name="postcode" id="postcode" minlength="4" maxlength="4" size="10" pattern="^[0-9]{4}$" required="required" /></p> 
 

 
      <p><label for="email">*Email:</label> 
 
      <input type="email" name="email" id="email" size="30" required="required" /></p> 
 

 
      <p><label for="number">*Phone number:</label> 
 
      <input type="tel" name="number" id="number" minlength="8" maxlength="12" size="10" required="required" /></p> 
 

 
      <p>Skill list:</p> 
 
      <p><label for="C/C+">C/C+</label> 
 
      <input type="checkbox" id="C/C+" name="category[]" checked="checked" /></p> 
 
      <p><label for="XML">XML</label> 
 
      <input type="checkbox" id="XML" name="category[]" /></p> 
 
      <p><label for="Java">Java</label> 
 
      <input type="checkbox" id="Java" name="category[]" /></p> 
 
      <p><label for="Python">Python</label> 
 
      <input type="checkbox" id="Python" name="category[]" /></p> 
 
      <p><label for="SQL">SQL</label> 
 
      <input type="checkbox" id="SQL" name="category[]" /></p> 
 
      <p><label for="PERL">PERL</label> 
 
      <input type="checkbox" id="PERL" name="category[]" /></p> 
 
      <p><label for="MySQL">MySQL</label> 
 
      <input type="checkbox" id="MySQL" name="category[]" /></p> 
 
      <p><label for="Windows">Windows</label> 
 
      <input type="checkbox" id="Windows" name="category[]" /></p> 
 
      <p><label for="UNIX">UNIX</label> 
 
      <input type="checkbox" id="UNIX" name="category[]" /></p> 
 
      <p><label for="Linux">Linux</label> 
 
      <input type="checkbox" id="Linux" name="category[]" /></p> 
 
      <p><label for="other">Other Skills:</label> 
 
      <input type="checkbox" id="other" name="category[]" /></p> 
 
      <textarea id="other" name="other" rows="8" cols="70" placeholder="Please write any other skills you may have here..."></textarea> 
 
      </p> 
 
     </fieldset> 
 
     </fieldset> 
 
     <input type="submit" value="Apply" /> 
 
     <input type="reset" value="Reset Application" /> 
 
    </form> 
 
    </article> 
 
</body> 
 

 
</html>

+0

、あなたは結果郵便番号のための年齢のための偽と真得れば、それはtrueを返しますか?はい? – prabushitha

+0

はい、年齢if文が郵便番号に関係しませんif文 –

答えて

0

あなたは他のチェックボックスとテキスト領域の重複したIDを持っています。それを修正してください。 これが機能している可能性があります。あなたのコードに応じて

"use strict"; 
 
/*get variables from form and check rules*/ 
 
function validate() { 
 
    var errMsg = ""; /* stores the error message */ 
 
    var result = true; /* assumes no errors */ 
 

 
    var other = document.getElementById("other").checked; 
 
    //get varibles from form and check rules here 
 
    // if something is wrong set result = false, and concatenate error message 
 

 
    var dob = document.getElementById("dob").value.split("/"); 
 
    var date = new Date(dob[2], parseInt(dob[1]) - 1, dob[0]); 
 
    var today = new Date(); 
 
    var age = today.getFullYear() - date.getFullYear(); 
 

 
    if (age >= 80) { // Outside age ranges. 
 
    errMsg = errMsg + "You must be 80 or younger to apply for this job\n"; 
 
    result = false; 
 
    } 
 

 
    if (age <= 15) { // Outside age ranges. 
 
    errMsg = errMsg + "You must be 15 or older to apply for this job\n"; 
 
    result = false; 
 
    } 
 

 
var postcode = document.getElementById("postcode").value; 
 
var state = document.getElementById("state").options[document.getElementById("state").selectedIndex].text; 
 

 
var regex; 
 
//VIC = 3 OR 8, NSW = 1 OR 2 ,QLD = 4 OR 9 ,NT = 0 ,WA = 6 ,SA=5 ,TAS=7 ,ACT= 0. 
 
switch (state) { 
 
    case "Please Select": 
 
     return false; 
 
    case "VIC": 
 
     regex = new RegExp(/(3|8)\d+/); 
 
     break; 
 
    case "NSW": 
 
     regex = new RegExp(/(1|2)\d+/); 
 
     break; 
 
    case "QLD": 
 
     regex = new RegExp(/(4|9)\d+/); 
 
     break; 
 
    case "NT": 
 
     regex = new RegExp(/0\d+/); 
 
     break; 
 
    case "WA": 
 
     regex = new RegExp(/6\d+/); 
 
     break; 
 
    case "SA": 
 
     regex = new RegExp(/5\d+/); 
 
     break; 
 
    case "TAS": 
 
     regex = new RegExp(/7\d+/); 
 
     break; 
 
    case "ACT": 
 
     regex = new RegExp(/0\d+/); 
 
     break; 
 
} 
 
if(!postcode.match(regex)){ 
 
    errMsg = errMsg + "State and postcode do not match\n"; 
 
    result = false; 
 
} 
 

 
    if (other && document.getElementById("otherText").value.trim().length===0) { 
 
    errMsg = errMsg + "You have selected other skills, you must enter one other skill in the text box\n"; 
 
    result = false; 
 
    } 
 

 
    if (errMsg != "") { //only display message box if there is something to show 
 
    alert(errMsg); 
 
    } 
 
    return result; //if false the information will not be sent to the server 
 
} 
 

 
function init() { 
 

 
    var regForm = document.getElementById("regform"); // get ref to the HTML element 
 

 
    regForm.onsubmit = validate; //register the event listener 
 
} 
 

 
window.onload = init;
<!DOCTYPE html> 
 
<html lang="en"> 
 

 
<head> 
 
    <meta charset="utf-8" /> 
 
    <meta name="description" content="Prototype Website" /> 
 
    <meta name="keywords" content="HTML, CSS, JavaScript" /> 
 
    <title>The Virtual World</title> 
 
    <link href="styles/style.css" rel="stylesheet" /> 
 
    <script src="apply.js"></script> 
 
</head> 
 

 
<body> 
 
    <article> 
 
    <header> 
 
     <h1>The Virtual World</h1> 
 

 
     <nav> 
 
     <p class="menu"><a href="index.html#home">Home</a></p> 
 
     <p class="menu"><a href="jobs.html#jobs">Jobs</a></p> 
 
     <p class="menu"><a href="apply.html#apply">Apply</a></p> 
 
     <p class="menu"><a href="about.html#about">About</a></p> 
 
     <p class="menu"><a href="enhancements.html#enhancements">Enhancements</a></p> 
 
     </nav> 
 
    </header> 
 
    <section id="required"> 
 
     <h5>All fields with * are required</h5> 
 
    </section> 
 
    <form id="regform" method="post" action="http://mercury.swin.edu.au/it000000/formtest.php"> 
 
     <fieldset> 
 
     <legend>Job Application</legend> 
 
     <p><label for="JobID">*Job ID:</label> 
 
      <input type="text" name="JobID" id="JobID" maxlength="5" size="10" pattern="^[a-zA-Z]{2}[0-9]{3}$" required="required" /></p> 
 
     <section id="choose"> 
 
      <h5>Please choose from QM593 or CS197</h5> 
 
     </section> 
 
     <fieldset> 
 
      <legend>Personal Details</legend> 
 
      <p><label for="title">*Title:</label> 
 
      <select name="title" id="title" required="required"> 
 
      <option value="">Please Select</option>   
 
      <option value="title">Dr</option> 
 
      <option value="title">Mr</option> 
 
      <option value="title">Miss</option> 
 
      <option value="title">Mrs</option> 
 
      <option value="title">Ms</option> 
 
     \t </select></p> 
 

 
      <p><label for="firstName">*First Name:</label> 
 
      <input type="text" name="firstName" id="firstName" maxlength="20" size="20" pattern="^[a-zA-Z ]+$" required="required" /> 
 

 
      <label for="familyName">*Family Name:</label> 
 
      <input type="text" name="familyName" id="familyName" maxlength="20" size="20" pattern="^[a-zA-Z ]+$" required="required" /></p> 
 

 
      <p><label for="midName">Middle Name:</label> 
 
      <input type="text" name="midName" id="midName" maxlength="20" size="20" pattern="^[a-zA-Z ]+$" /></p> 
 

 
      <p><label for="dob">*Date of Birth:</label> 
 
      <input type="text" name="dob" id="dob" placeholder="dd/mm/yyyy" pattern="\d{1,2}/\d{1,2}/\d{4}" maxlength="10" size="10" required="required" /></p> 
 

 
      <p>*Gender: 
 
      <label for="male">Male</label> 
 
      <input type="radio" id="male" name="sex" value="male" required="required" /> 
 
      <label for="female">Female</label> 
 
      <input type="radio" id="female" name="sex" value="female" required="required" /></p> 
 

 
      <p><label for="street">*Street Address:</label> 
 
      <input type="text" name="street" id="street" maxlength="40" size="30" required="required" /></p> 
 

 
      <p><label for="suburb">*Suburb/town:</label> 
 
      <input type="text" name="suburb" id="suburb" maxlength="40" size="20" required="required" /></p> 
 

 
      <p><label for="state">*State:</label> 
 
      <select name="state" id="state" required="required"> 
 
      <option value="">Please Select</option>   
 
      <option value="state">VIC</option> 
 
      <option value="state">NSW</option> 
 
      <option value="state">QLD</option> 
 
      <option value="state">NT</option> 
 
      <option value="state">WA</option> 
 
      <option value="state">SA</option> 
 
      <option value="state">TAS</option> 
 
      <option value="state">ACT</option> 
 
     \t </select></p> 
 

 
      <p><label for="postcode">*Postcode:</label> 
 
      <input type="text" name="postcode" id="postcode" minlength="4" maxlength="4" size="10" pattern="^[0-9]{4}$" required="required" /></p> 
 

 
      <p><label for="email">*Email:</label> 
 
      <input type="email" name="email" id="email" size="30" required="required" /></p> 
 

 
      <p><label for="number">*Phone number:</label> 
 
      <input type="tel" name="number" id="number" minlength="8" maxlength="12" size="10" required="required" /></p> 
 

 
      <p>Skill list:</p> 
 
      <p><label for="C/C+">C/C+</label> 
 
      <input type="checkbox" id="C/C+" name="category[]" checked="checked" /></p> 
 
      <p><label for="XML">XML</label> 
 
      <input type="checkbox" id="XML" name="category[]" /></p> 
 
      <p><label for="Java">Java</label> 
 
      <input type="checkbox" id="Java" name="category[]" /></p> 
 
      <p><label for="Python">Python</label> 
 
      <input type="checkbox" id="Python" name="category[]" /></p> 
 
      <p><label for="SQL">SQL</label> 
 
      <input type="checkbox" id="SQL" name="category[]" /></p> 
 
      <p><label for="PERL">PERL</label> 
 
      <input type="checkbox" id="PERL" name="category[]" /></p> 
 
      <p><label for="MySQL">MySQL</label> 
 
      <input type="checkbox" id="MySQL" name="category[]" /></p> 
 
      <p><label for="Windows">Windows</label> 
 
      <input type="checkbox" id="Windows" name="category[]" /></p> 
 
      <p><label for="UNIX">UNIX</label> 
 
      <input type="checkbox" id="UNIX" name="category[]" /></p> 
 
      <p><label for="Linux">Linux</label> 
 
      <input type="checkbox" id="Linux" name="category[]" /></p> 
 
      <p><label for="other">Other Skills:</label> 
 
      <input type="checkbox" id="other" name="category[]" /></p> 
 
      <textarea id="otherText" name="other" rows="8" cols="70" placeholder="Please write any other skills you may have here..."></textarea> 
 
      </p> 
 
     </fieldset> 
 
     </fieldset> 
 
     <input type="submit" value="Apply" /> 
 
     <input type="reset" value="Reset Application" /> 
 
    </form> 
 
    </article> 
 
</body> 
 

 
</html>

+0

重複IDを表示してもありがとうございますが、依然としてポストコード検証が機能しません。 –

+0

ロジックに失敗したテストケースを投稿できますか? – prabushitha

+0

@evanはフィドルを実行して今すぐチェックしてください – prabushitha

関連する問題