javascript - How do I enable cancel button with form validation? -
i'm using frmvalidator
javascript found here code.
i had create custom validation because i'm validating 2 input fields same id (i'm using array). validation working, problem though when click cancel
button, validation still takes effect meaning can't close dialog box
unless fill in fields.
here's code:
<form name="updateindustry" action="addindustry.jsp" method="get"> <table align="center"> <c:foreach var="row" items="${paramvalues.industries}"> <jsp:setproperty property="tableid" name="bean" value="${row}"/> <c:set var="ctr" value="${ctr + 1}"/> <input type="hidden" name="hello" value="${row}"/> <tr><th>industry name</th> <td><div id='updateindustry_industryname_errorloc' style="color:red;"></div> <input type="text" name="industryname" size=40 value="${bean.thisindustry.industryname}"/></td></tr> </c:foreach> <input type="hidden" name="finalcount" value="${ctr}"/> <tr><td style="text-align:center" colspan="3"> <input type=submit name="submit" value="update industry" onclick="btnupdate_click()"/> <input type=submit name="submit" value="cancel" onclick="btncancel_click()"/></td></tr> </table> </form>
and script
:
<script language="javascript"> var frmvalidator = new validator("updateindustry"); frmvalidator.enableonpageerrordisplay(); frmvalidator.enablemsgstogether(); function btnupdate_click(){ frmvalidator.setaddnlvalidationfunction(finalvalidator); } function btncancel_click(){ frmvalidator.clearallvalidations(); window.close(); } </script>
and here's finalvalidator
function, validation happens.
function finalvalidator() { var myform = document.forms["updateindustry"]; if (myform.industryname.length > 0) { for(var i=0; <= myform.industryname.length; i++) { if(myform.industryname[i].value.length == 0) { sfm_show_error_msg('please enter industry name.', myform.industryname[i]); return false; } } } return true; }
how can cancel
button work? in advance help! :)
$(document).ready(function () { $("#yourbuttonid").click(function () { error = false; $(".span1").remove(); if ($("#yourtextboxid").val() == "") { $("#yourtextboxid").after("<span class='span1'>write error</span>"); error = true; } }); });
try this.
Comments
Post a Comment