2014-10-12 49 views
-1

我已經嘗試過locahost,它是好的,因爲我用ftp將文件複製到我的網站作爲我的任務,它不工作....是否有以下代碼的任何問題?JavaScript表單驗證不工作在網站上

這裏是我的XHTML代碼形式:

</p><form enctype="multipart/form-data" method="post" action="insert.php" onsubmit="return validateForm()" name="register" > 

    <table style="width: 300px;"> 
    <tbody> 
    <tr> 

    <td>Username: * </td> 

    <td><input type="text" name="username" /></td> <<one of name forms 
    </tr> 
    <tr> 

    <td>Password: * </td> 

    <td><input type="text" name="password" /></td> <<name of forms 
    </tr> 
    <tr> 

    <td>Email: * </td> 

    <td><input type="text" name="email" /></td> <<name of forms 
    </tr> 
    <tr> 
    <td> * is must fulfilled </td> 
    </tr> 
    <tr> 
    <td><button value="Submit" type="submit">Submit</button></td> 
    <td><button value="Reset" type="reset">Clear</button></td></tr></tbody></table> 
      </form> 

,這裏是我的javascript

function validateForm() { 
     var y = document.forms["register"]["username"].value; 
    if (y == null || y == "") { 
     alert("Username must be filled out"); 
     return false; 
    } 
     var z = document.forms["register"]["password"].value; 
    if (z == null || z == "") { 
     alert("Password must be filled out"); 
     return false; 
    } 
    var x = document.forms["register"]["email"].value; 
    var atpos = x.indexOf("@"); 
    var dotpos = x.lastIndexOf("."); 
    if (atpos< 1 || dotpos<atpos+2 || dotpos+2>=x.length) { 
     alert("Not a valid e-mail address"); 
     return false; 
    } 
} 
+0

是JavaScript嵌入或從文件導入?如果它被導入,則可能是在將文件移動到服務器後路徑被破壞。 – dramzy 2014-10-12 04:21:16

+0

「*不起作用*」究竟意味着什麼? – 2014-10-12 04:31:59

+0

javascript是外部文件。它不工作意味着提交按鈕指向insert.php而不是首先驗證頁面 – 2014-10-12 04:46:34

回答

0
<form enctype="multipart/form-data" method="post" action="insert.php" name="register" > 

    <table style="width: 300px;"> 
    <tbody> 
    <tr> 

    <td>Username: * </td> 

    <td><input type="text" name="username" required/></td> <one of name forms 
    </tr> 
    <tr> 

    <td>Password: * </td> 

    <td><input type="text" name="password" required/></td> <name of forms 
    </tr> 
    <tr> 

    <td>Email: * </td> 

    <td><input type="text" name="email" pattern="^[A-Z0-9._%+-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}$./" required></td> <name of forms 
    </tr> 
    <tr> 
    <td> * is must fulfilled </td> 
    </tr> 
    <tr> 
    <td><button value="Submit" type="submit">Submit</button></td> 
    <td><button value="Reset" type="reset">Clear</button></td></tr></tbody></table> 
      </form> 
+0

@Timothy Herry Susanto,你可以在html中進行驗證,你不需要validationForm函數,你有嗎? – 4EACH 2014-10-12 18:15:21