2014-09-12 112 views
0

我在使用外部Java腳本時遇到問題。由於表單驗證效果不佳,我犯了一些錯誤。當java腳本代碼是內部的時候它表現不錯。當我把它放在外部文件中時,它什麼都不做。有時它工作。有時候不會。我正在使用net beans IDE。這是代碼。外部JavaScript-沒有做好

HTML

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <script type="text/javascript" src="myscript.js" ></script> 
    <link rel="icon" type="image/png" href="favicon.jpg"> 
</head> 
<body> 
<div class="main"> 
    <div class="header"></div> 
    <img src="dms_logo.gif" style="align:center;"> 
    <div class="continer"> 

     <div class="myForm1" style="height:200px; width:300px; float:left;"> 
      <h2>MTC</h2> 
      <form name="myForm1" action="access.jsp" method="get" onsubmit="return validateMtc()"> 
       <br/>MSISDN:<input type="text" name="MISDN" maxlength="11"> 
       <br/>RECIEPIENT:<input type="text" name="recipient" maxlength="12"> 
       <br/>DONOR:<input type="text" name="donor" maxlength="10"> 
       <br/>Date:<input type="text" name="date" value=""> 
       <br/>RN#:<input type="text" name="rn" value="" maxlength="5"> 
       <br/>Issue:<input type="text" name="Issue"> 
       <br/>SR:<input type="text" name="SR" maxlength="14"> 

       <br/><input type="submit" value="Submit"> 
       <br/><input type="reset" name="Reset"> 


      </form> 

      <form name="search1" action="search_misdn.jsp" method="get"> 
       <br/><input type="submit" value="Search"> 
       <input type="text" value="Enter MSISDN" name="search"> 
      </form> 
     </div> 
     <div class="myform2" style="float:left; height:200px; width:300px;"> 
      <h2>O.C.S</h2> 
      <form name="myForm2" action="ocs.jsp" method="post" onsubmit="return validate_ocs()"> 
       <br/><span>MSISDN:<input type="text" name="MISDN" maxlength="11"></span> 
       <br/><span>SR:<input type="text" name="SR" maxlength="14"></span> 
       <br/><span>REASON:<input type="text" name="reason"></span> 
       <br/><input type="submit" value="Submit"> 
       <br/><input type="reset" name="Reset"> 

      </form> 
      <form name="search1" action="search_misdn.jsp" method="get"> 
       <br/><input type="submit" value="Search"> 
       <input type="text" value="Enter MSISDN" name="search"> 
      </form> 
     </div> 
     <div class="myform3" style="float:left; height:200px; width:300px;"> 
      <h2>K.B.C</h2> 
      <form name="myForm3" action="kbc.jsp" method="post" onsubmit="validate_kbc()"> 
       <br/>MSISDN:<input type="text" name="MISDN" maxlength="11"> 
       <br/>SR:<input type="text" name="SR" maxlength="14"> 
       <br/><input type="submit" value="Submit"> 
       <br/><input type="reset" value="Reset"> 

      </form> 
      <form name="search1" action="search_misdn.jsp" method="get"> 
       <br/><input type="submit" value="Search"> 
       <input type="text" value="Enter MSISDN" name="search"> 
      </form> 
     </div> 

    </div> 
</div> 



</body> 

myscript.js

//alert(1); 
var numbers = /^[0-9]+$/; 
var Datee=/^[0-9-]*$/ig;///^\d{1, 2}\/\d{1, 2}\/\d{4}$/;  
var alpha_Numeric=/^[a-zA-Z0-9]*$/ig; 
var alpha=/^[A-Za-z]+$/; 
function validateMtc() 
{ 

var a = document.forms["myForm1"]["MISDN"].value; 
var b = document.forms["myForm1"]["Issue"].value; 
var c = document.forms["myForm1"]["SR"].value; 
var d = document.forms["myForm1"]["date"].value; 
var e = document.forms["myForm1"]["recipient"].value; 
var f = document.forms["myForm1"]["donor"].value; 
var g = document.forms["myForm1"]["rn"].value; 

if(a == "" || b == "" || c == "" || d == "" || e="" || f="" || g="") 
{ 
    alert("Manadotry fields should not left blank"); 
    document.myForm1.MISDN.focus(); 
    document.myForm1.Issue.focus(); 
    document.myForm1.SR.focus(); 
    document.myForm1.date.focus(); 
    document.myForm1.recipient.focus(); 
    document.myForm1.donor.focus(); 
    document.myForm1.rn.focus(); 
    return false; 
} 
else if(!a.match(numbers)) 
{ 
    alert('Please input numeric characters only in MSISDN field'); 
    document.myForm1.MISDN.focus(); 
    return false; 
} 
else if(!b.match(alpha_Numeric)) 
{ 
    alert('Please input numeric and alphabets only in ISSUE field'); 
    document.myForm1.Issue.focus(); 
    return false; 
} 
else if(!c.match(numbers)) 
{ 
    alert('Please input numeric characters only SR field'); 
    document.myForm1.SR.focus(); 
    return false; 
} 
else if(!d.match(Datee)) 
{ 
    alert('Please input correct date in DATE field (dd-mm-yy)'); 
    document.myForm1.date.focus(); 
    return false; 
} 
else if(!e.match(alpha)) 
{ 
    alert('Please input aplhabets only in reciepeint filed'); 
    document.myForm1.recipient.focus(); 
    return false; 

} 
else if(!f.match(alpha)) 
{ 
    alert('Please input aplhabets only in donor field'); 
    document.myForm1.donor.focus(); 
    return false; 

} 
else if(!g.match(alpha_Numeric)) 
{ 
    alert('Please input aplhabets and numerics only in RN# field'); 
    document.myForm1.rn.focus(); 
    return false; 

} 
else 
    return true; 

} 
function validate_ocs() 
{ 
var h = document.forms["myForm2"]["MISDN"].value; 
var i = document.forms["myForm2"]["SR"].value; 
var j = document.forms["myForm2"]["reason"].value; 


if(h == "" || i == "" || j == "") 
{ 
    alert("Manadotry fields should not left blank"); 
    document.myForm2.MISDN.focus(); 
    document.myForm2.reason.focus(); 
    document.myForm2.SR.focus(); 
    return false; 
} 
else if(!h.match(numbers)) 
{ 
    alert('Please input numeric characters only in MSISDN field'); 
    document.myForm2.MISDN.focus(); 
    return false; 
} 
else if(!i.match(numbers)) 
{ 
    alert('Please input numeric characters only SR field'); 
    document.myForm2.SR.focus(); 
    return false; 
} 
else if(!(j.match(alpha_Numeric))) 
{ 
    alert('Please input numeric and alphabets only in ISSUE field'); 
    document.myForm2.Issue.focus(); 
    return false; 
} 
else 
    return true; 

} 
function validate_kbc() 
{ 
var k = document.forms["myForm3"]["MSISDN"].value; 
var l = document.forms["myForm3"]["SR"].value; 
if(k == "" || l == "") 
{ 
    alert("Manadotry fields should not left blank"); 
    document.myForm1.MISDN.focus(); 
    document.myForm1.SR.focus(); 

    return false; 
} 
else if(!k.match(numbers)) 
{ 
    alert('Please input numeric characters only in MSISDN field'); 
    document.myForm3.MISDN.focus(); 
    return false; 
} 
else if(!l.match(numbers)) 
{ 
    alert('Please input numeric characters only in SR field'); 
    document.myForm3.SR.focus(); 
    return false; 
} 
else 
    return true; 

} 
//alert('!!!'); 
+1

它被稱爲JavaScript,而不是Java腳本或Java腳本。調用後者可能會令人困惑,特別是在使用java服務器端時。 – 2014-09-12 15:28:05

+2

一次只能關注一個以上的字段...... – epascarello 2014-09-12 15:28:45

回答

0

你myscript.js文件的位置可能是錯的。

仔細檢查您是否給予正確的位置。 嘗試通過瀏覽器訪問myscript.js文件。 打開控制檯(f12)並查看是否出現丟失文件的錯誤。

+0

它與所有文件所在的路徑相同。控制檯幫助我解決一些其他錯誤。我有一個新問題,當我輸入數據時,我留下空白的myform3字段,它顯示空字段的警告框。但在警告框上單擊確定就可以將空記錄存儲在數據庫中。爲什麼它不僅停留在該頁面上?並且不在DB中存儲空記錄 – ABJ 2014-09-12 16:13:43

1

•首先,你在網上有一個錯誤17.

if(a == "" || b == "" || c == "" || d == "" || e="" || f="" || g="") 

E = 「」,應通過電子郵件== 「」。其餘的變量也是如此。

當你比較東西時,你需要使用雙等於(或最好是三倍)。 見文檔:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators

•此外,你應該考慮的開發工具它配備了所有主要的瀏覽器,這些天。他們有錯誤控制檯,通常會在簡單情況下立即指出您的問題。 https://developer.chrome.com/devtools

•作爲附註。
JavaScript是一個單詞。調用它'java腳本'是混亂和錯誤的。

+0

控制檯幫助我解決一些其他錯誤。我有一個新問題,當我輸入數據時,我留下空白的myform3字段,它顯示空字段的警告框。但在警告框上單擊確定就可以將空記錄存儲在數據庫中。爲什麼它不僅停留在該頁面上?而不是在數據庫中存儲空記錄 – ABJ 2014-09-12 16:16:44

+0

您必須將代碼更新爲您目前擁有的代碼,但我無法真正瞭解您的意思。 – 2014-09-12 16:43:53

+0

我的意思是第三種形式,當文本字段爲空,並單擊提交按鈕時,會顯示一個警告框,提示有關丟失字段的信息。但是,當我點擊確定該警報框時,它會消失,當前頁面將重定向到另一頁(不好)。警報框消失後,它必須保留在該頁面上。只有這種情況發生在第三種形式,而不是前兩種形式。我希望你現在得到這個? – ABJ 2014-09-12 16:58:28