javascript - Why is my Java script code not running? (Form Validation) -
this 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 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 valid name. 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 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 comment."); return false; } } function validateform() { if (myname == null || myname == "" || myemail == null || myemail == "" || mycomment = null || mycomment == "") { alert("please fill out of fields!"); } }
it's not working @ all, if try alert in 1 of function doesn't show up. tried adding in validator.js
instead of calling events in html, , it's still not working.
document.getelementbyid("name").onblur = checkname; document.getelementbyid("email").onblur = checkemail; document.getelementbyid("comment").onblur = checkcomment;
debugged. in jsfiddle it's still not working, @ least there aren't mistakes in 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 valid name. 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 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 comment."); return false; } } function validateform() { if (myname === null || myname === "" || myemail === null || myemail === "" || mycomment === null || mycomment === "") { //=== old: == alert("please fill out of fields!"); } }
Comments
Post a Comment