Ris Ris - 3 months ago 9
HTML Question

Preventing form submit based on entering the same numbers in the box input

I have an input box here

<input type="text" size="9" maxlength="9" id="my_account" name="my_account" value="" >


and here I am disallowing users to enter the same numbers in the box, but what I really want is to prevent the form submit instead

var inputNode = document.getElementById('my_account');

inputNode.addEventListener('keydown', (event) => {
var inputValue = event.key;
var inputNodeValue = inputNode.value;
var length = inputNodeValue.length;

if (length === 3 && inputNodeValue[0] === inputValue) {
event.preventDefault();
}
});


this is my form prevent default

$("form#customer-summary").submit(function(e){
e.preventDefault();
alert("prevent submit");
});


How can I combine these two parts so I dont allow users to submit same numbers in the box ?

Answer
var inputNode = document.getElementById('my_account');
var customer_summary = document.getElementById('customer-summary');
customer_summary.addEventListener('submit',function(e){
   if(!is_unique(inputNode.value)){
      e.preventDefault();
      alert("prevent submit");
   }             
});

    function is_unique(val){
       for(var i=0;i<val.length;i++){
           if(val.indexOf(val.charAt(i)) != val.lastIndexOf(val.charAt(i))){
              return false;
           }       
       }
      return true;
    }

I think this should do the trick . Please upvote if found relevant. Workng jsfiddle:https://jsfiddle.net/m31h1zhg/2/