jquery - Not sure why my div is not showing validation messages -
ello stack, cant seem figure out why there isn't text showing in div after validation of field. here code. know validation running but, i'm not sure why validation message isn't showing in div.
<html> <head> <script src="scripts/jquery-1.5.2.js" type="text/javascript"></script> <script src="http://localhost:62147/scripts/jquery.validate.js" type="text/javascript"></script> <script type="text/javascript"> "use strict"; $(document).ready(function () { $("#testerbtn").bind("click", function () { alert("onclick fired"); $("#myform").validate({ errorlabelcontainer: $("#errors"), rules: { texttoval: { required: true } }, messages: { texttoval: "the field required! nub~!" } }); alert("validation should have happend."); }) }); </script> <title>test page</title> </head> <body> <form id = "myform" action="htmlpage1.htm"> <div id="errors"></div> <div> <input type="text" id="texttoval" value="" /> <input type="button" id="testerbtn" value="tester" /> </div> </form> </body> </html>
there couple of things see here causing issues:
ensure
inputs havenameattributes:<input type="text" id="texttoval" value="" name="texttoval" />validate()should instantiated outside of click handler. additionally, changebuttontypesubmitautomatically trigger validation code.
in summary:
javascript:
$(document).ready(function () { $("#myform").validate({ errorlabelcontainer: $("#errors"), rules: { texttoval: { required: true } }, messages: { texttoval: "the field required! nub~!" } }); }); html:
<form id = "myform" action="htmlpage1.htm"> <div id="errors"></div> <div> <input type="text" id="texttoval" value="" name="texttoval" /> <input type="submit" id="testerbtn" value="tester" /> </div> </form> working example: http://jsfiddle.net/fj8xf/
Comments
Post a Comment