Created
February 6, 2014 11:00
-
-
Save craigpearson/8842141 to your computer and use it in GitHub Desktop.
IE8 & IE9 Validate in conjunction with placeholder fix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
$('[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().parents('form').submit(function() { | |
$(this).find('[placeholder]').each(function() { | |
var input = $(this); | |
if (input.val() == input.attr('placeholder')) { | |
input.val(''); | |
} | |
}); | |
}); | |
$.validator.addMethod('notPlaceholder', function(val, el) { | |
return this.optional(el) || ( val !== $(el).attr('placeholder') ); | |
}, $.validator.messages.required); | |
$('#quick-quote').validate({ | |
rules: { | |
clientname: { | |
minlength: 3, | |
required: true, | |
notPlaceholder: true | |
}, | |
housenumber: { | |
minlength: 1, | |
required: true, | |
notPlaceholder: true | |
}, | |
postcode: { | |
minlength: 3, | |
required: true, | |
notPlaceholder: true | |
}, | |
telephone: { | |
digits: true, | |
required: true, | |
notPlaceholder: true | |
}, | |
email: { | |
minlength: 4, | |
required: true, | |
email: true, | |
notPlaceholder: true | |
} | |
}, | |
highlight: function(element) { | |
$(element).closest('.form-group').addClass('has-error'); | |
}, | |
unhighlight: function(element) { | |
$(element).closest('.form-group').removeClass('has-error'); | |
}, | |
errorElement: 'span', | |
errorClass: 'help-block', | |
errorPlacement: function(error, element) { | |
if(element.parent('.input-group').length) { | |
error.insertAfter(element.parent()); | |
} else { | |
error.insertAfter(element); | |
} | |
} | |
}); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment