let btn = $("#btnSend"); let form = $("#zeForm"); let input = $("#zeInput"); let msg = $("#msg"); let allowedValues = ["atown", "btown", "ctown"]; // same values as the options in your datalist btn.on('click' , function() { let allGood = false; allowedValues.each(function(index, element) { if (element === input.value) { allGood = true; return; } }) if (allGood) { msg.text("Success!!"); msg.attr('style',"color:green"); //form.submit(); } else { msg.text("This value is not accepted"; msg.attr('style',"color:red"); } msg.attr('style',"display:inline"); });