Reputation: 483
I'm trying to validate my html 5 form that uses placeholders in IE7 + 8.
I used the valid() call from the Validation plugin for jquery. That means I cant use this solution: placeholder issue with jQuery Validate. My problem is the same though. The placeholder script I use adds the placeholder text to the value attribute. When the validation is called the script reads the placeholder text as valid input.
I don't do full validation because the form is spread over several 'tabs' so needs to be validated before the next section is shown. This is non negotionalble
My placeholder script:
$('[placeholder]').focus(function() {
var input = $(this);
if (input.val() == input.attr('placeholder')) {
input.val('');
input.removeClass('placeholder');
}
}).blur(function() {
var input = $(this);
if (input.val() == '' || input.val() == input.attr('placeholder')) {
input.addClass('placeholder');
input.val(input.attr('placeholder'));
}
}).blur();
$('[placeholder]').parents('form').submit(function() {
$(this).find('[placeholder]').each(function() {
var input = $(this);
if (input.val() == input.attr('placeholder')) {
input.val('');
}
})
});
and here is where I call the validation:
if (elements.valid() === 0) {
valid = 0;
}
if (valid) {
$('.form').removeClass('error');
$(this).closest('section').hide().next().show();
}
Any help would be greatly appreciated!
Upvotes: 0
Views: 1267
Reputation: 53
You can try to redefine the default required function of validator. Before calling the validation add this:
jQuery.validator.addMethod("required", function(value, element, param) {
// check if dependency is met
if ( !this.depend(param, element) ) {
return "dependency-mismatch";
}
if ( element.nodeName.toLowerCase() === "select" ) {
// could be an array for select-multiple or a string, both are fine this way
var val = $(element).val();
return val && val.length > 0;
}
if ( this.checkable(element) ) {
return this.getLength(value, element) > 0;
}
default:
var placeholderval = $(element).attr('placeholder');
//if some placeholder values are actually default values, just use "default" attribute to mark them
var defaultvar = ($(element).attr('default') === undefined);
return ($.trim(value).length > 0 && ($.trim(value)!=$.trim(placeholderval) || !defaultvar));
}, jQuery.validator.messages.required);
Upvotes: 2