-3

我对 js 完全陌生,我为@大学的一个项目制作了这个表格,但 js 似乎根本不起作用。我没有收到任何错误,它只是不起作用!我启用了 Javascript,并且完全禁用了无脚本。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <link rel="stylesheet" type="text/css" href="<?php echo $_SESSION['css'];?>"/>


 <script type="text/javascript">


  function validate_f() {
    var result=true;
    var username=document.getElemenetById('username').value;
    var illegalChars = /\W/;
    var x=document.getElemenetById('email').value;
        var pwd=document.getElemenetById('password').value;


  if (illegalChars.test(username) || username.length>25) {
        result=false;

        alert("Username must be latin chars and not more than 25 characters");
  }

if (x==null || x=="" || username == null || username=="" || pwd == null || pwd == "" || afm == null || afm == "" ) {
result=false;
alert("you have to fill up all the Boxes dude");
}
    return result;  

} 
<div id="main">
<div id="formcontainer">
<form id="data" name="data" action="connection.php" method="POST" on submit="return validate_f()">
<table width="100%" cellpadding="3" cellspacing="2">
<tr>
    <td width="25%"> </td>
</tr>
<tr>
    <td class="right" width="20%"> <strong> Username </strong> </td>
    <td>
    <input type="text" name="username" id="username" size="25" maxlength="25"/>
</td>
</tr>
<tr>
    <td class="right"> <strong> Password</strong> </td>
    <td>
    <input type="password" name="password" id="password" size="25" maxlength="25"/>
</td>
</tr>
<tr>
    <td class="right"> <strong> Email</strong> </td>
    <td>
    <input type="text" name="email" id="email"/>
    </td>
</tr>
<?php
/*
<tr>
<td class="right"> <strong> Security Code: </strong> </td>
<td>
<img src="CaptchaSecurityImages.php" alt="" />
<input id="security_code" name="security_code" type="text" />
</td>
</tr>
*/
?>
<tr>   
 <td>
          <input name="reset" type="reset" id="reset" value="Reset" />
          <input name="submit" type="submit" id="submit" value="Submit"/>

        </td>
</tr>
</table>
</form>
</div>

</div>
4

4 回答 4

2

onsubmit 事件之间没有空格

<form id="data" name="data" action="connection.php" method="POST" onsubmit="return validate_f()">
于 2012-06-06T05:01:05.283 回答
1

on & submit 之间有一个空格。试试这行表单标签...

<form id="data" name="data" action="connection.php" method="POST" onsubmit="return validate_f()">

并且

  1. div's应该在<form>标签内。
  2. </script>在开始之前关闭标签<form>
  3. </head>在启动标签之前关闭<form>标签。
  4. 关闭</html>html 页面末尾的标签。
于 2012-06-06T05:02:50.627 回答
1

有拼写错误——

document.getElemenetById('password').value
var username=document.getElemenetById('username').value;

应该

document.getElementById('password').value
var username=document.getElementById('username').value;

参考:https ://developer.mozilla.org/en/DOM/document.getElementById

于 2012-06-06T05:03:03.500 回答
0

尝试这个,

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  <link rel="stylesheet" type="text/css" href="<?php echo $_SESSION['css'];?>"/> 


 <script type="text/javascript">


     function validate_f() {
         var result = true;
         var username = document.getElemenetById('username').value;
         var illegalChars = /\W/;
         var x = document.getElemenetById('email').value;
         var pwd = document.getElemenetById('password').value;



         if (illegalChars.test(username) || username.length > 25) {
             result = false;

             alert("Username must be latin chars and not more than 25 characters");
         }

         if (x == null || x == "" || username == null || username == "" || pwd == null || pwd == "" || afm == null || afm == "") {
             result = false;
             alert("you have to fill up all the Boxes dude");
         }
         return result;

     }
</script> 
</head>


<body><div id="main">
<div id="formcontainer">
<form id="data" name="data" action="connection.php" method="POST" on submit="return validate_f()"> 
<table width="100%" cellpadding="3" cellspacing="2"> 
<tr> 
    <td width="25%"> </td> 
</tr> 
<tr> 
    <td class="right" width="20%"> <strong> Username </strong> </td> 
    <td> 
    <input type="text" name="username" id="username" size="25" maxlength="25"/> 
</td> 
</tr> 
<tr> 
    <td class="right"> <strong> Password</strong> </td> 
    <td> 
    <input type="password" name="password" id="password" size="25" maxlength="25"/> 
</td> 
</tr> 
<tr> 
    <td class="right"> <strong> Email</strong> </td> 
    <td> 
    <input type="text" name="email" id="email"/> 
    </td> 
</tr> 
<?php 

<tr> 
<td class="right"> <strong> Security Code: </strong> </td> 
<td> 
<img src="CaptchaSecurityImages.php" alt="" /> 
<input id="security_code" name="security_code" type="text" /> 
</td> 
</tr> 


<tr>    
 <td> 
          <input name="reset" type="reset" id="reset" value="Reset" /> 
          <input name="submit" type="submit" id="submit" value="Submit"/> 

        </td> 
</tr> 
</table> 
</form> 
</div> 

</div> 
</body>

</html>
于 2012-06-06T07:24:24.820 回答