Jquery Validation Watermark
-
26-09-2019 - |
Question
I am using jQuery validation plugin and Watermark plugin. The problem I am facing is when i applied a watermark to any input field for which validation is also applied then validations are failing
For Example:
<input type=text name=myinput class="required">
and I applied a watermark for this field in document ready function:
$("#myinput ").Watermark("myinput ");
This is the sample case where validation is failing as value=myinput
is set for input field which is watermarked.
Solution
Finally i got the solution for this by registering a method for validation rules and using that along with the class for that input tag like
$.validator.addMethod("watermark", Watermark, $.validator.messages["required"]);
function Watermark(value, element) {
if (element.className.match("required") != null) {
var id = "WM_" + element.name;
return value != document.getElementById(id).value;
}
else
return true;
}
And for input tag use this
<input type='text' class='required watermark'>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow