2013-03-31 39 views
-2

我不是很懂Javascript,我嘗試了一切,但找不到解決方案。將驗證錯誤文本放在輸入字段下方

我使用這個插件進行驗證,它完美的作品,但只是一個小問題是,它寫入錯誤文本持續顯示旁邊的輸入字段,我希望它表明波紋管輸入字段。任何人都可以修改這個腳本?非常感謝你:)

;(function($, window, document, undefined){ 

    // our plugin constructor 
    var SimpleValidate = function(elem, options) { 
    this.elem = elem; 
    this.$elem = $(elem); 
    this.options = options; 
    this.metadata = this.$elem.data('plugin-options'); 
    this.$requiredInputs = this.$elem.find(':input.required'); 
    }; 

    // the plugin prototype 
    SimpleValidate.prototype = { 
    defaults: { 
     errorClass: 'error', 
     errorText: 'Please fill out this field.', 
     emailErrorText: 'Please enter a valid E-mail', 
     errorElement: 'strong', 
     removeLabelChar: '*', 
     inputErrorClass: 'input-error', 
     completeCallback: '', 
     ajaxRequest: false 
    }, 

    init: function() { 
     var self = this; 

     // Introduce defaults that can be extended either 
     // globally or using an object literal. 
     self.config = $.extend({}, self.defaults, self.options, self.metadata); 

     // What type of error message is it 
     self.errorMsgType = self.config.errorText.search(/{label}/); 
     self.emailErrorMsgType = self.config.emailErrorText.search(/{label}/); 

     self.$elem.on('submit.simpleValidate', $.proxy(self.handleSubmit, self)); 

     return this; 
    }, 

    checkField: function(index) { 
     var self = this; 
     var $field = self.$requiredInputs.eq(index); 
     var fieldValue = $.trim($field.val()); 
     var labelText = $field.siblings('label').text().replace(self.config.removeLabelChar, ''); 
     var errorMsg = ''; 

     //Check if it's empty or an invalid email and format the error message 
     if(fieldValue === '') { 
     errorMsg = self.formatErrorMsg(self.config.errorText, labelText, self.errorMsgType); 
     self.hasError = true; 
     } else if($field.hasClass('email')) { 
     if(!(/^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/.test(fieldValue.toLowerCase()))) { 
      errorMsg = self.formatErrorMsg(self.config.emailErrorText, labelText, self.emailErrorMsgType); 
      self.hasError = true; 
     } 
     } 

     //If there is an error, display it 
     if(errorMsg !== '') { 
     $field.addClass(self.config.inputErrorClass).after('<' + self.config.errorElement + ' class="' + self.config.errorClass + '">' + errorMsg + '</' + self.config.errorElement + '>'); 
     } 
    }, 

    formatErrorMsg: function(errorText, labelText, errorMsgType) { 
     return (errorMsgType > -1) ? errorText.replace('{label}', labelText) : errorText; 
    }, 

    handleSubmit: function(e) { 
     var self = this; 

     // We are just starting, so there are no errors yet 
     this.hasError = false; 

     // Reset existing displayed errors 
     self.$elem.find(self.config.errorElement + '.' + self.config.errorClass).remove(); 
     self.$elem.find(':input.' + self.config.inputErrorClass).removeClass(self.config.inputErrorClass); 

     // Check each field 
     self.$requiredInputs.each($.proxy(self.checkField, self)); 

     // Don't submit the form if there are errors 
     if(self.hasError) { 
     e.preventDefault(); 
     } else if(self.config.completeCallback !== '') { // If there is a callback 
     self.config.completeCallback(self.$elem); 

     // If AJAX request 
     if(self.config.ajaxRequest) { 
      e.preventDefault(); 
     } 
     } 
    } 
    }; 

    SimpleValidate.defaults = SimpleValidate.prototype.defaults; 

    $.fn.simpleValidate = function(options) { 
    return this.each(function() { 
     new SimpleValidate(this, options).init(); 
    }); 
    }; 

})(jQuery, window , document); 
+1

_ NOP ......告訴我們,到目前爲止,它爲什麼沒有工作,你已經嘗試過的東西,並附加一個活例如,如果可能的話,你可能會得到一些幫助。 – elclanrs

回答

0

添加到您的CSS:

.error { display: block; } 

或更改ErrorElementdiv

new SimpleValidate(this, {errorElement: 'div'}).init(); 
+0

試過這個,就像一個魅力。非常感謝你 – FallenRider

0

當你調用這個,只是通過在參數:errorElement: 'p'。缺省值是使用strong,這是一個內聯元素。

0

只需更換

$field.addClass(self.config.inputErrorClass).after('<' + self.config.errorElement + ' class="' + self.config.errorClass + '">' + errorMsg + '</' + self.config.errorElement + '>') 

有:

$field.addClass(self.config.inputErrorClass).after('<p><' + self.config.errorElement + ' class="' + self.config.errorClass + '">' + errorMsg + '</' + self.config.errorElement + '></p>') 
+0

應該選擇正確的errorElement,或者設置正確的css風格。不改變插件的核心功能。 – Robert

+0

試過這個和羅伯特建議的css事情,兩個工作:)非常感謝,你救了我:p – FallenRider

+0

你是對的羅伯特。 –

0

不幸的是,我不能重現你的代碼,因此它是我很難說該怎麼做。然而,在我看來,你應該解決這一段代碼:

$field.addClass(self.config.inputErrorClass).after('<' + self.config.errorElement + ' class="' + self.config.errorClass + '">' + errorMsg + '</' + self.config.errorElement + '>'); 

如果我理解正確的,它的輸入以後添加錯誤消息。也許你可以使用類似:「任何人都可以修改此腳本,這是」

$field.addClass(self.config.inputErrorClass).after('<br>').after('<' + self.config.errorElement + ' class="' + self.config.errorClass + '">' + errorMsg + '</' + self.config.errorElement + '>');