仅使用Jquery验证的字母 [英] Letters only using Jquery Validation

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

问题描述

我尝试了一篇文章中的所有解决方案两个三个但在我的示例项目中,我仍然可以在名字字段中输入数字或spec char。

I tried every solution from articles one, two , three yet in my sample project, I can still enter a number or spec char in the first name field.

我试过的六件事:

$.validator.setDefaults({ submitHandler: function(form) { form.submit(); } });

jQuery.validator.addMethod("regex", function(value, element, param) { return value.match(new RegExp("^" + param + "$")); });
var ALPHA_REGEX = "[a-zA-Z]*";

jQuery.validator.addMethod("accept", function(value, element, param) {
  return value.match(new RegExp("." + param + "$"));
});

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

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

}

  $(document).ready(function() {
    $('#contact_form').bootstrapValidator({
                  fields: {
            first_name: 
            {
                validators: {
                field: { accept: "[a-zA-Z]+" },
                regex: ALPHA_REGEX,
                 lettersonly:true,
                required: true,
                pattern: "^[a-zA-Z_]*$",
                        stringLength: {
                        min: 2,

                    },
                        notEmpty: {
                        message: 'Please supply your first name',
                        callback: function(value, validator, $field) {
                            if (!isValid(value)) {
                              return {
                                valid: false,
                              };
                            }
                            else
                            {
                              return {
                                valid: true,
                              };    

                    }
                }


            },

我确定我遗漏了一些简单或可能使用稍微过时的jQuery库的东西?编辑:我实际上只是尝试使用其他版本的库没有成功。

I am sure I am missing something simple or possibly using a slightly outdated jQuery Library? I actually just tried using other versions of the library to no success.

推荐答案

我建议你试试你的REGEX 正则表达式匹配器,只需在谷歌搜索。

I suggest you to try your REGEX on some "Regex Matcher", just search it on google.

在你拥有正确的REGEX之后,请确保你将它传递给你的功能,而不是另一个......

After you have the right REGEX, make sure you are passing it to your function, and not another one...

这就是所有的解决方案。

That's all the solution.

祝你好运!

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

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