2011-12-19 71 views
0

工作,我想驗證使用佔位符在IE7 + 8佔位符在IE與jQuery的驗證

我使用的有效()調用從驗證的jQuery插件我的HTML 5的形式。這意味着我不能使用這個解決方案:placeholder issue with jQuery Validate。雖然我的問題是相同的。我使用的佔位符腳本將佔位符文本添加到值屬性中。當調用驗證時,腳本將佔位符文本讀取爲有效輸入。

我不做全面驗證,因爲表單分佈在多個「選項卡」中,因此需要在下一節顯示之前進行驗證。這是不可negotionalble

我的佔位符腳本:

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

和這裏就是我所說的驗證:

if (elements.valid() === 0) { 
    valid = 0; 
} 

if (valid) { 
    $('.form').removeClass('error'); 
    $(this).closest('section').hide().next().show(); 
} 

任何幫助將不勝感激!

+0

嘗試[驗證佔位符的通用方法] [1] [1]:http://stackoverflow.com/a/15898568/657357 – Hogan 2013-04-09 10:18:19

回答

2

您可以嘗試重新定義驗證器的默認所需函數。撥打電話之前驗證補充一點:

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);