为什么我的 Java 脚本代码不是 运行? (表单验证)
Why is my Java script code not running? (Form Validation)
这是我的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>
这是我的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!");
}
}
它根本不起作用,即使我尝试在其中一个功能中发出警告,它也没有出现。我还尝试在 validator.js
中添加它而不是在 HTML 中调用事件,但它仍然无法正常工作。
document.getElementById("name").onblur = checkname;
document.getElementById("email").onblur = checkemail;
document.getElementById("comment").onblur = checkcomment;
而不是使用 if (myComment == "")
使用 if (myComment.value == "")
你的代码有很多错误,你的一些变量是 undefined
在浏览器中按 F12 并打开“控制台”选项卡。这将向您显示 Javascript.
中的所有语法错误
已调试。在 JSFiddle 中它仍然无法正常工作,但至少你的 JavaScript 中没有错误了。在 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!");
}
}
这是我的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>
这是我的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!");
}
}
它根本不起作用,即使我尝试在其中一个功能中发出警告,它也没有出现。我还尝试在 validator.js
中添加它而不是在 HTML 中调用事件,但它仍然无法正常工作。
document.getElementById("name").onblur = checkname;
document.getElementById("email").onblur = checkemail;
document.getElementById("comment").onblur = checkcomment;
而不是使用 if (myComment == "")
使用 if (myComment.value == "")
你的代码有很多错误,你的一些变量是 undefined
在浏览器中按 F12 并打开“控制台”选项卡。这将向您显示 Javascript.
中的所有语法错误已调试。在 JSFiddle 中它仍然无法正常工作,但至少你的 JavaScript 中没有错误了。在 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!");
}
}