user3286854
user3286854

Reputation: 25

Why is my JavaScript code not running? (Form Validation)

This is my 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:

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. [email protected]");
        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.

document.getElementById("name").onblur = checkname;
document.getElementById("email").onblur = checkemail;
document.getElementById("comment").onblur = checkcomment;

Upvotes: 0

Views: 73

Answers (3)

Coder
Coder

Reputation: 302

Debugged. In JSFiddle it's still not working, but at least there aren't mistakes in your JavaScript anymore. Tried in 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. [email protected]");
        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!");
    }
}

Upvotes: 1

Rajeev Goel
Rajeev Goel

Reputation: 1523

Press F12 in your browser and open up the Console tab. That will show you all the syntax errors in your Javascript.

Upvotes: 0

Radonirina Maminiaina
Radonirina Maminiaina

Reputation: 7004

Instead of using if (myComment == "") use if (myComment.value == "") And there is many error into your code, some of your variables is undefined

Upvotes: 0

Related Questions