jQuery select2插件正则表达式允许的字符? [英] jquery select2 plugin regex for allowed characters?

查看:58
本文介绍了jQuery select2插件正则表达式允许的字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有为用户输入定义一组允许的"字符?例如仅字母数字输入.似乎在官方文档中找不到它

Is there anyway to define a set of "allowed" characters for user input? For e.g. alphanumeric input only. Can't seem to find it within the official documentation

推荐答案

您可以使用createSearchChoice选项来完成此操作,该选项允许您定义自定义函数以根据用户输入创建标签.要不允许标签包含字母数字字符以外的字符,select2应该初始化为:

You can accomplish this using the createSearchChoice option, which allows you to define a custom function to create tags from user input. To not allow tags containing other than alphanumeric characters, the select2 should be initialized as:

$("#mySelect").select2({
            createSearchChoice: function(term) {
                if(term.match(/^[a-zA-Z0-9]+$/g))
                    return { id: term, text: term };
            },
            formatNoMatches: "Enter valid format text"})

如果用户正在键入包含非字母数字字符的文本,则select2下方的下拉列表将不显示任何匹配项.不必指定自定义formatNoMatches,但比默认的未找到匹配项"更能弄清楚为什么他们的输入不被接受,这对用户更有用.

If the user is typing text that contains non-alphanumeric characters, the dropdown below the select2 will show no matches. It's not necessary to specify a custom formatNoMatches but it is more helpful for the user than the default "No matches found" to figure out why their input isn't being accepted.

更新:

从select2版本〜4.0开始,此功能现在称为createTag

As of select2 version ~ 4.0 this function is now called createTag

这篇关于jQuery select2插件正则表达式允许的字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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