[英]Why is my Java script code not running? (Form Validation)
This is my HTML: 这是我的HTML:
<html>
<head>
<title>Article</title>
<link rel="stylesheet" href="pop.css" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<script type="text/javascript" src="validator.js"></script>
<div id="comment_form">
<form method="POST">
Name:
<input type="text" name="name" id="name1" onblur="checkname()">
<br/> Email:
<input type="text" name="email" id="email1" onblur="checkemail()">
<br/>
<br/> Comment:
<br/>
<textarea name="comment" id="comment1" cols="50" rows="2" onblur="checkcomment()"></textarea>
<br/>
<input type="submit" value="comment" onsubmit="validateform()">
</form>
</body>
</html>
and this is my Javascript: 这是我的Javascript:
function checkname() {
alert("Isee ur java script");
var myName = document.getElementById("name1");
var pos = myName.value.search(/^[A-Z][a-z] $/);
if (pos != 0) {
alert("Please Enter a Valid name. It should not contain numbers.");
retrun false;
} else
return true
}
function checkemail() {
var myEmail = document.getElementById("email1");
var pos = myEmail.value.search(/^[a-z]+-?\.?_?@[a-z]+\.[a-z]+$/);
if (pos != 0) {
alert("Please Enter a Valid Email Address. eg. youremail@hotmail.com");
retrun false;
} else
return true
}
function checkcomment() {
var myComment = document.getElementById("comment1");
if (myComment == null || myComment == "") {
alert("Please add a comment.");
return false;
}
}
function validateform() {
if (myName == null || myName == "" || myEmail == null || myEmail == "" || myComment = null || myComment == "") {
alert("Please fill out all of the fields!");
}
}
it's not working at all, even if I try to alert something in one of the function it doesn't show up. 即使我试图在其中一个功能中未显示的内容发出警报,它也根本不起作用。 I also tried adding this in
validator.js
instead of calling the events in HTML, and it's still not working. 我还尝试将其添加到
validator.js
而不是在HTML中调用事件,但仍然无法正常工作。
document.getElementById("name").onblur = checkname;
document.getElementById("email").onblur = checkemail;
document.getElementById("comment").onblur = checkcomment;
Debugged. 调试。 In JSFiddle it's still not working, but at least there aren't mistakes in your JavaScript anymore.
在JSFiddle中,它仍然无法正常工作,但是至少您的JavaScript中没有错误了。 Tried in JSBin:---> http://jsbin.com/vabewefubi/1/edit?html,js,output
在JSBin中尝试过:---> http://jsbin.com/vabewefubi/1/edit?html,js,output
function checkname() {
alert("Isee ur java script");
var myName = document.getElementById("name1");
var pos = myName.value.search(/^[A-Z][a-z] $/);
if (pos !== 0) { //!== old: !=
alert("Please Enter a Valid name. It should not contain numbers.");
return false; //return old: retrun
} else
return true; //missing semicolon
}
function checkemail() {
var myEmail = document.getElementById("email1");
var pos = myEmail.value.search(/^[a-z]+-?\.?_?@[a-z]+\.[a-z]+$/);
if (pos !== 0) { //!== old: !=
alert("Please Enter a Valid Email Address. eg. youremail@hotmail.com");
return false; //return old: retrun
} else
return true; //missing semicolon
}
function checkcomment() {
var myComment = document.getElementById("comment1");
if (myComment === null || myComment === "") { //=== old: ==
alert("Please add a comment.");
return false;
}
}
function validateform() {
if (myName === null || myName === "" || myEmail === null || myEmail === "" || myComment === null || myComment === "") { //=== old: ==
alert("Please fill out all of the fields!");
}
}
而不是使用if (myComment == "")
而是使用if (myComment.value == "")
并且代码中有很多错误,某些变量undefined
Press F12 in your browser and open up the Console tab. 在浏览器中按F12键,然后打开“控制台”选项卡。 That will show you all the syntax errors in your Javascript.
这将向您显示Javascript中的所有语法错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.