2014-10-12 62 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