How can I add custom conditional validation code to a jQuery event handler? -
i've got jquery in ascx file (sharepoint webpart) responds checkbox's changed event, manipulating properties of control/element based on state of checkbox (checked or unchecked, question):
$(document).on("change", '[id$=ckbxemp]', function () { var ckd = this.checked; var $input = $('[id$=txtbxssnoritin]'); if(ckd) $input.data("oldvalue", $input.val()); // remember current value $input.prop("maxlength", ckd? 4 : 12).css({ background: ckd? 'yellow' : "lightgreen", width: ckd? 40 : 80 }).val(function(i, v){ // if checked, slice value 4 chars: return ckd && v.length>4 ? v.slice(0,4) : $input.data("oldvalue"); }); }); the script above can run this fiddle, based on answer got here
what yet need add custom validation code. if in employee (ssn) mode (the checkbox checked), guess need verify 4 chars in 0..9 subset entered; if itin, though (the checkbox unchecked), need more complicated validation. how incorporate that? need write validation functions , call them "val" function? iow, should line:
return ckd && v.length>4 ? v.slice(0,4) : $input.data("oldvalue"); ...be changed like:
return ckd && v.length>4 ? getssn($input.data) : getitin($input.data); ...and code functions so:
function getssn(var entry) { // validate/verify, slice 4 chars if necessary } function getitin(var entry) { // validate/verify, slice 12 if necessary } ...or what?
update
come think of it, seems logical (in fact, downright obvious) need create event handler textbox, (pseudoscript):
$(document).on("change", '[id$=txtbxssnoritin]', function () { var $input = $('[id$=txtbxssnoritin]'); var ckbxchecked = "#ckbxemp".checked; if (ckbxchecked) { // validate ssn, passing $input } else { // validate itin, passing $input } }); but "how there here" question...
Comments
Post a Comment