将验证错误文本放在输入字段下方

时间:2013-03-31 22:46:15

标签: javascript jquery forms validation positioning

我真的不懂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 );

5 个答案:

答案 0 :(得分:0)

当你调用它时,只需传入参数:errorElement: 'p'。默认是使用strong,这是一个内联元素。

答案 1 :(得分: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>')

答案 2 :(得分:0)

将此添加到您的CSS:

.error { display: block; }

或者将ErrorElement更改为div

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

答案 3 :(得分:0)

一个简单的解决方案可能是将errorElement: 'strong'更改为block element,例如errorElement: 'p'。您还需要确保style the elements properly with CSS

如果您想学习JavaScript,我建议您查看Eloquent JavaScript

答案 4 :(得分: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 + '>');