每次我尝试运行这个 xhtml 文件时,它只显示 javascript 文件,它不运行该函数。我究竟做错了什么?为什么函数不运行?我对 JS 和 xhtml 相当陌生,因此欢迎和感谢任何和所有帮助。这是我的代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!--Submit form, Assignment 2-->
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<script type = "text/javaScript" src = "mss58.js"></script>
<title> Submit Form Assignment </title>
<meta charset="UTF-8">
</head>
<body>
<h1>Please Fill out the following Form</h1>
<form name ="SubmitForm" action = "mss58.js" onsubmit = "validate()" method="run">
Name: <input type ="text" name="firstname"><br />
Email address: <input type = "text" name="emailaddress"><br />
Password: <input type = "password" name = "pwd"><br />
Confirm Password: <input type = "password" name = "cpwd"><br />
Gender: <input type = "radio" name = "gender" value = "male">Male
<input type = "radio" name = "gender" value = "female">Female<br />
<br />
Comments: <br />
<textarea rows ="10" cols = "50"></textarea><br />
<input type = "submit" value = "Submit">
</form>
</body>
</html>
编辑:我得到了正确提交的表单,但是我的 javascript 文件不想检查。我猜我的 if 语句不起作用。当我输入无效的密码、用户名或电子邮件时没有弹出警报时,有人可以试一试并告诉我为什么吗?我确定这很明显,我只是在这门课之前从未使用过 javascript
function validate(){
var name = document.forms["SubmitForm"]["firstname"].value;
var email = document.forms["SubmitForm"]["emailaddress"].value;
var password = document.forms["SubmitForm"]["pwd"].value;
var cpass = document.forms["SubmitForm"]["cpwd"].value;
var arrloc = email.indexOf("@");
var perloc = email.lastIndexOf(".");
var alph = false;
var num = false;
var sym = false;
if(name == null || name == ""){
alert("You must fill out the Name field");
return false;
}
if(arrloc < 1 || perloc < arrloc + 2 || perloc + 2 >= email.length){
alert("Email is not valid");
return false;
}
for(var i = 0, i < password.length-1, i++){
if(password.substring(i,i+1) == /[a-z]/){
alph = true;
}
if(password.substring(i,i+1) == /[A-Z]/){
alph = true;
}
if(password.substring(i,i+1) == /[0-9]/){
num = true;
}
if(password.substring(i,i+1) == /[^\w+$]/){
sym = true;
}
}
if(alph == true && num == true && sym == true){
return true;
}else{
alert("Your password is invalid!");
return false;
}
if(password == cpass){
return true;
}else{
alert("Your passwords do not match!");
return false;
}
};
validate();