0

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;
2
  • 1
    Check retrun false typo. Also you dont need the semicolon there. Commented Apr 29, 2015 at 15:28
  • 1
    Also myComment = null at the end should be myComment == null Commented Apr 29, 2015 at 15:29

3 Answers 3

1

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!");
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.