Question

Having a similar issue to this question

In all browsers the placeholder text is fine after a validation fail on my form but it disappears in IE8. Initially IE8 wasn't showing the placeholders so I used this fix to fill the placeholders

$('[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('');   
        }  
    })   
});

Should I be using a different placeholder fix/polyfill in the first place? or is there a way to fix this?

Was it helpful?

Solution

    var placeholders = {};
$('form').validate({
   submitHandler: function(form) {

   $(form).find(':input[placeholder]').each(function() {
      var placeholder = $(this).attr('placeholder'); 
      placeholders[placeholder] = this;
      $(this).removeAttr('placeholder');
   });       

   form.submit();


 },



enter code here

  $.each(placeholders, function(placeholder, element) {
      $(element).attr('placeholder', placeholder);
  });

}

});

OTHER TIPS

Try this library for placeholders in browsers that don't support placeholders.

https://github.com/mathiasbynens/jquery-placeholder

I solved this by redefining the default required function of validator, Before calling the validation I added 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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top