Sunday, 15 September 2013

javascript - Getting an Udefined value for a function in JS -



javascript - Getting an Udefined value for a function in JS -

getting undefined homecoming type method validatehallticket please check code & modify accordingly so, when click submit button should able appropriate homecoming types.

<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <title>4cubes site</title> <script type="text/javascript"> function validateform(form) { document.writeln(validatenames(form["firstname"])); document.writeln(validatehallticket(form["hallticket"])); // getting undefined value if (validatenames(form["firstname"]) && validatehallticket(form["hallticket"])) { form.sub`enter code here`mit(); } else { alert("please fill required fields"); } } function validatenames(inputfield) { names_help = document.getelementbyid('lastname_help'); if (inputfield.value.length == 0) { if (names_help!= null) { names_help.innerhtml = "please come in validate name"; homecoming false; } } else { names_help.innerhtml = ""; homecoming true; } } function validatehallticket(inputfield) { var regex = /^\d{2}k91a\d{4}$/; var rs = regex.test(inputfield.value); hallticket_help = document.getelementbyid('hallticket_help'); if (!regex.test(inputfield.value)) { if (hallticket_help != null) { hallticket_help.innerhtml = "enter valid hallticket"; homecoming false; } } else { hallticket_help.innerhtml = ""; homecoming true; } } </script> </head> <body> <center> <font face="arabic transparent" size="6" color="teal">4cubes college</font> </center> <br></br> <br></br> <form method="post" action="servlet.do" name="myform"> hallticket: <input type="text" name="hallticket" id="hallticket" onblur="validatehallticket(this)"></input> <span id="hallticket_help" style="color:red; font-style:italic;"> </span> <br></br> firstname: <input type="text" name="firstname" id="firstname" onblur="validatenames(this)"></input> <span id="firstname_help" style="color:red; font-style:italic;"> </span> <br></br> lastname: <input type="text" name="lastname" id="lastname" onblur="validatenames(this)"></input> <span id="lastname_help" style="font-style:italic; color:red;"> </span> <center> <input type="button" value="submit" onclick="validateform(this.form);"></input> </center> </form> </body> </html>

this because, function loads before dom loads. seek moving <script> tag before </body> , works.

notes

add return false; when invalid. add handler onsubmit event of <form> tag. full code <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <title>4cubes site</title> </head> <body> <center> <font face="arabic transparent" size="6" color="teal">4cubes college</font> </center> <br></br> <br></br> <form method="post" action="servlet.do" name="myform" onsubmit="return false;"> hallticket: <input type="text" name="hallticket" id="hallticket" onblur="validatehallticket(this)"></input> <span id="hallticket_help" style="color:red; font-style:italic;"> </span> <br></br> firstname: <input type="text" name="firstname" id="firstname" onblur="validatenames(this)"></input> <span id="firstname_help" style="color:red; font-style:italic;"> </span> <br></br> lastname: <input type="text" name="lastname" id="lastname" onblur="validatenames(this)"></input> <span id="lastname_help" style="font-style:italic; color:red;"> </span> <center> <input type="button" value="submit" onclick="return validateform(this.form);"></input> </center> </form> <script type="text/javascript"> function validateform(form) { document.writeln(validatenames(form["firstname"])); document.writeln(validatehallticket(form["hallticket"])); // getting undefined value if (validatenames(form["firstname"]) && validatehallticket(form["hallticket"])) { form.submit(); } else { alert("please fill required fields"); } homecoming false; } function validatenames(inputfield) { names_help = document.getelementbyid('lastname_help'); if (inputfield.value.length == 0) { if (names_help!= null) { names_help.innerhtml = "please come in validate name"; homecoming false; } } else { names_help.innerhtml = ""; homecoming true; } homecoming false; } function validatehallticket(inputfield) { var regex = /^\d{2}k91a\d{4}$/; var rs = regex.test(inputfield.value); hallticket_help = document.getelementbyid('hallticket_help'); if (!regex.test(inputfield.value)) { if (hallticket_help != null) { hallticket_help.innerhtml = "enter valid hallticket"; homecoming false; } } else { hallticket_help.innerhtml = ""; homecoming true; } homecoming false; } </script> </body> </html> fiddle: http://jsbin.com/apuyaw/1

javascript html

No comments:

Post a Comment