Passing an ID to an external .js file
-
Want to do clienside validation on a textbox control. This textbox can represent two data types depending on an associtate checkbox. So if checkbox is checked the validation should do (a) and if off do (b) In the javascript funtion below I do a getElementForID("chkBox1") However if I want this to be a global routine I don't know what the checkBox would be and preferably I'd like to pass an Integer so I could do multiple validations on this control or others. The "Sender" parameter seems to be my textbox, So question is how do I setup the ClientValidationFuntion property of the validator to pass an integer? My custom validator is setup like this <asp:CustomValidator ID="lsdCustomVal" ControlToValidate="txtBox1" runat="server" ClientValidationFunction= "validateLSDControl"></asp:CustomValidator> in my external js file I have >> function validateLSDControl( sender, args) { var expr1 = "[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9]"; var expr2 = "[a-z]-[0-9][0-9][0-9]-[A-Z]/[0-9][0-9]"; var x = document.getElementById(sender.controltovalidate); var chkbox = document.getElementById("chkBox1"); var smsg = ""; if (chkbox.checked) { var patt1 = new RegExp(expr, "g"); smsg = "expr1 Invalid"; } else { var patt1 = new RegExp(nts, "g"); smsg = "expr2 Invalid"; } var result = false; if (x != null) { var s = x.value; if (s.match(patt1)) { // alert("it worked"); result = true; } &nb
-
Want to do clienside validation on a textbox control. This textbox can represent two data types depending on an associtate checkbox. So if checkbox is checked the validation should do (a) and if off do (b) In the javascript funtion below I do a getElementForID("chkBox1") However if I want this to be a global routine I don't know what the checkBox would be and preferably I'd like to pass an Integer so I could do multiple validations on this control or others. The "Sender" parameter seems to be my textbox, So question is how do I setup the ClientValidationFuntion property of the validator to pass an integer? My custom validator is setup like this <asp:CustomValidator ID="lsdCustomVal" ControlToValidate="txtBox1" runat="server" ClientValidationFunction= "validateLSDControl"></asp:CustomValidator> in my external js file I have >> function validateLSDControl( sender, args) { var expr1 = "[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9]"; var expr2 = "[a-z]-[0-9][0-9][0-9]-[A-Z]/[0-9][0-9]"; var x = document.getElementById(sender.controltovalidate); var chkbox = document.getElementById("chkBox1"); var smsg = ""; if (chkbox.checked) { var patt1 = new RegExp(expr, "g"); smsg = "expr1 Invalid"; } else { var patt1 = new RegExp(nts, "g"); smsg = "expr2 Invalid"; } var result = false; if (x != null) { var s = x.value; if (s.match(patt1)) { // alert("it worked"); result = true; } &nb
if you are using masterpage then you must below code to access asp control like document.getElementById('<%= chkBox1.ClientID%>'); or change the form's default name according you and access the control by FORMNAME.GETELEMENTBYID plz check it.
Rajeev Kr. Sharma Sr. Software Engineer E-Meditek Solutions Ltd. http://www.emeditek.co.in/