How can i validate the input from a html5 Datalist?

后端 未结 4 1294
遥遥无期
遥遥无期 2020-12-30 12:45

I would like to know how I can validate the input value that comes from a Datalist. I mean, if I have a Datalist where the user can start to write

相关标签:
4条回答
  • 2020-12-30 12:52

    I'd like to share a non-jquery alternative, only in Js:

    function is_valid_datalist_value(idDataList, inputValue) {
      var option = document.querySelector("#" + idDataList + " option[value='" + inputValue + "']");
      if (option != null) {
        return option.value.length > 0;
      }
      return false;
    }
    
    function doValidate() {
      if (is_valid_datalist_value('colours', document.getElementById('color').value)) {
        alert("Valid");
      } else {
        alert("Invalid");
      }
    }
    <form onsubmit="return false">
      <input type="text" id="color" list="colours">
      <datalist id="colours">
        <option value="Red" data-id="1" />
        <option value="Blue" data-id="2" />
        <option value="Green" data-id="3" />
        <option value="Black" data-id="4" />
        <option value="White" data-id="5" />
        </datalist>
      <button onclick="doValidate();">Send</button>
    </form>

    0 讨论(0)
  • 2020-12-30 13:05

    If you use jQuery find method, it will traverse the DOM tree trying to find the correct element taking into account the value of one of its attributes, and looking at your comment, I think that you are concerned about performance.

    Your first idea about iterate over all the options and check the value property is better (speaking about performance) than traversing the DOM tree looking for an element with a particular value in one of their attributes (look at this comparison). You need to be aware that shorter code is not the same as faster code.

    A faster solution is to generate an array of strings at the beginning and search the correct value inside it in the validation process:

    //---At the beginning of your application
    let list = Array.prototype.map.call(document.getElementById("colours").options, (option) => option.value);
    
    //---Later in your validation process
    if (list.indexOf(value) < 0) {
        //Invalid
    }
    

    Another faster solution is to generate an object at the beginning and use it as a hash map, checking for the correct value inside it in the validation process:

    //---At the beginning of your application
    let hashmap = Array.prototype.reduce.call(document.getElementById("colours").options, (obj, option) => {
        if (!obj[option.value]) { obj[option.value] = true; }
        return obj;
    }, {});
    
    //---Later in your validation process
    if (!hashmap[value]) {
        //Invalid
    }
    

    Here you have the four methods compared in measurethat:

    1 - Your first idea (iterate over all the Datalist options)

    2 - Use the jQuery find method (@nsthethunderbolt solution)

    3 - Create an array of strings at the beginning and search the value in the Array in the validation process

    4 - Create a hash map at the beginning and check if the value is true in the validation process

    https://www.measurethat.net/Benchmarks/Show/4430/0/search-an-option-inside-a-datalist

    0 讨论(0)
  • 2020-12-30 13:06

    Try this:

    <input type="text" list="colours" id='txt'>
    

    And on form submit you can check:

    var val = $("#txt").val();
    
    var obj = $("#colours").find("option[value='" + val + "']");
    
    if(obj != null && obj.length > 0)
        alert("valid");  // allow form submission
    else
        alert("invalid"); // don't allow form submission
    
    0 讨论(0)
  • 2020-12-30 13:17

    You can do this with HTML5 validation using pattern. It's easier if you're populating your datalist with some sort of template, but it would look something like this (Note that you would need additional code to handle the validation - I just added very simple CSS to display the validation state)

    input:valid {
      border: 1px solid green;
    }
    input:invalid {
      border: 1px solid red;
    }
    <input type="text" list="colours"
    pattern="^(Red|Blue|Green|Black|White)$"
    >
    
    <datalist id="colours">
        <option value="Red" data-id="1">
        <option value="Blue" data-id="2">
        <option value="Green" data-id="3">
        <option value="Black" data-id="4">
        <option value="White" data-id="5">
    </datalist>

    0 讨论(0)
提交回复
热议问题