每次我尝试运行这个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();
答案 0 :(得分:0)
您应该将您的javascript代码包含在<script>
标记中。
为了运行定义的函数,你可以调用它,例如,像这样:validate()
答案 1 :(得分:0)
您需要更改
<form name ="SubmitForm" action = "mss58.js" onsubmit = "validate()" method="run">
到
<form name ="SubmitForm" action = "TARGET_PAGE.php" onsubmit = "return validate()" method="POST">
在JavaScript验证函数中,它应该在数据不正确时返回false,否则返回true。