简体   繁体   English

如何在文本输入事件模糊时手动触发 BootStrap 验证

[英]How to manually trigger BootStrap Validation on blur of a text input event

I am using Bootstrap validation for my Application .我正在为我的 Application 使用 Bootstrap 验证。

Incase if user enters "abc" under text input how to show a validation message , Entered value is not allowed如果用户在文本输入下输入“abc”如何显示验证消息,则不允许输入值

This is my code这是我的代码

$('#taginsertform').bootstrapValidator(
{
        feedbackIcons:
        {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
        },
        fields:
        {
                recipientname:
                {
                        feedbackIcons: 'false',
                        validators:
                        {
                                notEmpty:
                                {
                                        message: 'Reciepnt Name cannot be empty'
                                }
                        }
                }
        }
}).on('success.form.bv', function(e)
{
        e.preventDefault();
        addTagSbmt();
});

function addTagSbmt()
{
   $('#taginsertform').bootstrapValidator('resetForm', true);
        $('.closemodal').trigger('click');

        return false;
}
$('#myModal').on('shown.bs.modal', function()
{
        $('#taginsertform').bootstrapValidator('resetForm', true);
});


$( "#recipientname" ).blur(function() {

  var recipientnameval = $("#recipientname").val();

  recipientnameval = recipientnameval.toLowerCase();

  if(recipientnameval==='abc')
  {
  alert(recipientnameval);
  // fire bootstrap valdation as invalid data 
  }

});

In the below jsfiddle , upon entering abc under text input how can i show a message ?在下面的 jsfiddle 中,在文本输入下输入 abc 后,如何显示消息? http://jsfiddle.net/xmrxbL9f/97/ http://jsfiddle.net/xmrxbL9f/97/

In short, no there is no built in bootstrap functionality to do the validation.简而言之,没有内置的引导功能来进行验证。

Bootstrap simply provides styles to the default HTML input types , HTML input attributes and JavaScript constraints validation functionality. Bootstrap 只是为默认的HTML 输入类型HTML 输入属性JavaScript 约束验证功能提供样式。 In order to validate an element we call the checkValidity() JavaScript method.为了验证element我们调用checkValidity() JavaScript 方法。

If you are following the BootStrap Form Validation documentation then we can accomplish an onblur event validation with the following snippet.如果您遵循BootStrap 表单验证文档,那么我们可以使用以下代码段完成onblur事件验证。

$(function () { // jQuery ready
    // On blur validation listener for form elements
    $('.needs-validation').find('input,select,textarea').on('focusout', function () {
        // check element validity and change class
        $(this).removeClass('is-valid is-invalid')
               .addClass(this.checkValidity() ? 'is-valid' : 'is-invalid');
    });
});

This example will set the is-valid or is-invalid class on the current element when it looses focus.此示例将在当前元素失去焦点时在当前元素上设置is-validis-invalid类。 Note we are assigning event listeners to all input,select,textarea in all forms with the class needs-validation .请注意,我们将事件侦听器分配给所有表单中的所有input,select,textarea ,类needs-validation

If you wish to trigger a different type of notification the same can be applied.如果您希望触发不同类型的通知,则可以应用相同的方法。

nJoy!快乐!

You can append the error div on blur.您可以在模糊上附加错误 div。 Try it if it matches your requirement.如果它符合您的要求,请尝试一下。

 $( "#recipientname" ).blur(function() {

  var recipientnameval = $("#recipientname").val();

 recipientnameval = recipientnameval.toLowerCase();
 var errorDiv = "<div class='form-error alert alert-danger fade in'>Please Insert valid data<div>"
 if(recipientnameval==='abc')
  {
   $('.form-group').append(errorDiv);
  }else{
   $('.form-error').remove();
  }
});

Best of luck :)祝你好运:)

Add an id to the form as formId将 id 添加到表单中作为 formId

var formEl = $("#formId");

formEl.find('input,select,textarea').on('focusout', function () {

    var thisEl = $(this).parent();
    if(!thisEl.hasClass('was-validated')){
        thisEl.addClass('was-validated');
    }

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM