I am working with an application that is generating HTML forms, and is including custom attributes to be used for client side validation. The error message to be used for validation, is included in the error attribute, and the expression to validate the input against, is included in the expression attribute.
I have the following javascript block,
$(document).ready(function() {
$("input[expression]").blur(function() {
var inputs = $(":input[expression]");
inputs.each(function(index){
var input = inputs[index];
var expression = $(this).attr('expression');
if(!$(this).val().match(expression))
{
var error = $(this).attr("error");
$(this).attr("title",error);
$(this).tooltip({ position: "center right", opacity: 0.7});
$(this).tooltip().show();
}
else
{
}
});
});
});
What I am attempting to do is setup all the fields with an expression attribute to validate their expression value against the current value when the input box, loses focus.
This is sort of working right now, but not with the behavior I would like it to have.
Right now, the tooltip error message is popping up when I click on the input element. I do not want this, I only want it to show the error message following running through the blur() callback function.
Is there a correct way to do this?
An example of what my markup would look like is shownbelow
<input type='text' name='firstName' expression='[a-zA-Z]' error='Please Enter First Name'/>