Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selectize.js Create: validate CREATE input

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?

like image 901
Learner Avatar asked Apr 13 '26 06:04

Learner


1 Answers

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">
like image 187
Swati Avatar answered Apr 15 '26 01:04

Swati



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!