使用bootstrap验证器验证表单字段以仅包含字母 [英] validate form field for containing only letters using bootstrap validator

查看:288
本文介绍了使用bootstrap验证器验证表单字段以仅包含字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用bootstrap验证表单字段,我正在学习如何通过以下操作来实现:
联系表单验证示例
在以下链接中。

This is the first time I use bootstrap for validate a form fields, I'm learning how to do it by following a
contact form validation example in the following link.

幸运的是,过了一段时间,我'我们在提供的链接中得到了一个例子,运行得很好。

Fortunately after some time, I've got the example in the link provided, running perfectly.

现在我想知道如何验证名称字段只包含字母。

基于以下代码段中的验证器:{} 对象...

Based on the validators:{} object in the code snippet below...

$(document).ready(function(){
    $('#form_id').bootstrapValidator({
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields:{
            first_name:{
                validators:{
                        stringLength: {
                        min: 2,
                    },
                        notEmpty: {
                        message: 'Please supply your first name'
                    }
                }
            }
        }
    })
});

引导程序中有类似的内容可以进行此类验证吗?

is there something similar in bootstrap that allows to make such validation?

推荐答案

你可以使用它。使用这个 codepen 我已经为firstname字段添加了回调,你也可以使用回调进行其他自定义需要在bootstrap验证。

you can use this. works this codepen I have added callback for just firstname field and you can also use callback for other custom needs in bootstrap validation.

详情参考: http://formvalidation.io / validators / callback /

function isValid(value)
{
  var fieldNum = /^[a-z]+$/i;

  if ((value.match(fieldNum))) {
      return true
  }
  else
  {
      return false
  }

}

如果用户输入字母以外的内容,则会触发消息。

message fires if user enter something out of letter.

first_name: {
                validators: {
                        stringLength: {
                        min: 2,
                    },
                        notEmpty: {
                        message: 'Please supply your first name'
                    },
                    callback: {
                        message: 'please enter only letters',
                        callback: function(value, validator, $field) {
                            if (!isValid(value)) {
                              return {
                                valid: false,
                              };
                            }
                            else
                            {
                              return {
                                valid: true,
                              };    
                            }

                        }
                    }
                }

       }

这篇关于使用bootstrap验证器验证表单字段以仅包含字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆