This is my selectize.js function
$("#addmobilenumbers").selectize({
inputClass: 'form-control selectize-input',
dropdownParent: "body",persist: false,
create: true //need to create and allow only digits
});
I want only numeric (to enter only mobile numbers) input to create here with min/max length. How can I do it?
You can check that condition inside your create function . Simply use $.isNumeric(input) to check if the value is number and add your min/max length if this condition statisfy create your new option else use return false; to prevent any default action.
Demo Code :
$("#addmobilenumbers").selectize({
inputClass: 'form-control selectize-input',
dropdownParent: "body",
persist: false,
plugins: ['remove_button'],
create: function(input) {
console.log($.isNumeric(input), input.length)
//check if no is numeric ,,and min/max length
if ($.isNumeric(input) && (input.length > 2 && input.length < 6)) {
return {
value: input,
text: input
} //create that tags...
} else {
console.log("can't add")
return false; //prevnt default...
}
}
});
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.8.5/css/selectize.css" />
<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.8.5/js/standalone/selectize.min.js"></script>
<input type="text" id="addmobilenumbers">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With