简体   繁体   English

为什么此xhtml文件显示JS代码但不运行该功能

[英]Why does this xhtml file display JS code but doesn't run the function

Every time I try to run this xhtml file, it only displays the javascript file, it does not run the function. 每次我尝试运行此xhtml文件时,它仅显示javascript文件,而不运行该功能。 What am I doing wrong? 我究竟做错了什么? Why the function does not run? 为什么功能无法运行? I'm fairly new to JS and xhtml so any and all help is welcomed and appreciated. 我对JS和xhtml相当陌生,因此欢迎您提供所有帮助。 Here is my code 这是我的代码

<!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>

EDIT: I got the form to submit correctly however my javascript file does not want to check. 编辑:我得到了正确提交的表格,但是我的JavaScript文件不想检查。 I'm guessing my if statements aren't work. 我猜我的if语句不起作用。 Can someone give it a once over and tell me why, when I type in an invalid password, username, or email that no alerts pop up? 有人可以给我一次机会,然后告诉我为什么,当我输入无效的密码,用户名或电子邮件时,没有弹出警报吗? I'm sure it's something obvious, I've just never used javascript before this class 我敢肯定这很明显,在上课之前我从未使用过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();

You should include your javascript code into <script> tag. 您应将您的JavaScript代码包含在<script>标记中。

In order to run the defined function, you've to call it, for example, like this: validate() 为了运行定义的函数,您必须调用它,例如,如下所示: validate()

You need to change 你需要改变

<form name ="SubmitForm" action = "mss58.js" onsubmit = "validate()" method="run">

to

<form name ="SubmitForm" action = "TARGET_PAGE.php" onsubmit = "return validate()" method="POST">

In your JavaScript validate function, it should return false when the data is incorrect and return true otherwise. 在您的JavaScript validate函数中,当数据不正确时,它应该返回false,否则返回true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM