I have an input text which already has some validate rules
<input type="text" value="" placeholder="Write the code here"
name="myCode" id="myCode" data-rule-required="true"
data-msg-required="Required field" class="form-control"
aria-required="true">
and have created a custom rule to validate the syntax of the code
$("#myCode").rules("add", { checkCode:
function () {
return $('input[name=codeText]').val();
}
});
With this method I am defining that myCode
should be validated by using also checkCode
method which in turns receive in input the value of codeText
.
This works really very well. However to maintain clean the code I would like to move the rule definition inside a data-*
attribute but I can't understand how to write the code to dynamically pass the equivalent of return $('input[name=codeText]').val()
. Please have a look at the question marks below.
<input type="text" value="" placeholder="Write the code here"
name="myCode" id="myCode"
data-rule-required="true" data-msg-required="Required field"
data-rule-checkCode="????" data-msg-checkCode="Invalid code"
class="form-control" aria-required="true">
What I have to put in the attribute value???
EDIT
As per Sparky answer I've ended up on putting all the selector on the attribute. Like this:
<input type="text" value="" placeholder="Write the code here"
[...]
data-rule-checkCode="input[name=codeText]"
[...]>
and using it like this in my method
$.validator.addMethod("checkCode", function(value, element, param) {
return $(param).val();
}, '...');