jquery - Selectize.js 创建:验证 CREATE 输入
问题描述
这是我的 selectize.js 函数
$("#addmobilenumbers").selectize({
inputClass: 'form-control selectize-input',
dropdownParent: "body",persist: false,
create: true //need to create and allow only digits
});
我只想numeric
(只输入手机号码)create
用min/max length
. 我该怎么做?
解决方案
使用createFilter
和onInitialize
选项
createFilter:在使用给定的正则表达式创建之前验证选项
/^[0-9]{10}$/ - accepts only numbers between 0-9 of length 10 digits
onInitialize : 用于设置 maxlength / 限制输入为数字的事件
this.$control_input.prop('maxlength', 10)
this.$control_input.on('input', function() {
this.value = this.value.replace(/[^\d]/g, '')
})
$("#addmobilenumbers").selectize({
inputClass: 'form-control selectize-input',
dropdownParent: "body",
persist: false,
create: true,
createFilter: /^[0-9]{10}$/,
onInitialize: function() {
this.$control_input.prop('maxlength', 10)
this.$control_input.on('input', function() {
this.value = this.value.replace(/[^\d]/g, '')
})
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.13.3/js/standalone/selectize.min.js" integrity="sha512-pF+DNRwavWMukUv/LyzDyDMn8U2uvqYQdJN0Zvilr6DDo/56xPDZdDoyPDYZRSL4aOKO/FGKXTpzDyQJ8je8Qw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.13.3/css/selectize.default.min.css" integrity="sha512-H955AcCOE/fUjX4XWkN0FwjCYVV/zioSF6VpUKCcrGdR1Wa8paFWYixWYp85npbnx3i1kZCH4Rm4TRxut2+d5A==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<select id="addmobilenumbers" multiple></select>
参考
推荐阅读
- tcp - TCP WiFi python-arduino通信问题
- python-3.x - 如何确定文件扩展名?
- sql - 尽管导入成功消息,但从 excel 导入的表未反映在数据库中
- axios - 无法使用 axios get 获取当前用户
- android - cameraX android在图像捕获时捕获更多区域
- javascript - 如何在本机反应中将填充物放在我的输入字符串上?
- python - 使用归并排序的反转次数
- r - 风筝图上的新线或边距变化?
- html - 垫子按钮样式不适用于垫子菜单
- django - django中是否有任何解决方案可以将外键列与模板内的主键进行比较,我需要在我的项目中进行特定匹配