为什么我的Java脚本代码没有运行? (表格确认)

时间:2015-04-29 15:25:37

标签: javascript html javascript-events

这是我的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;

1 个答案:

答案 0 :(得分:1)

调试。在JSFiddle中,它仍然不起作用,但至少你的JavaScript中没有错误。试过JSBin:---&gt; 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!");
    }
}