Search
 
SCRIPT & CODE EXAMPLE
 

HTML

disable submit button after form validation

<form action="#" method="post" id="myform" name="myform">
    Location: <input name="location" type="text" />
    Site: <input name="site" type="text" />
    Age: <input name="age" type="text" />
    Gender <input name="gender" type="text" />
    <input name="button" type="submit" class="myButton" id="button" value="Submit" />
</form>
Comment

disable submit button until form is fully validated

let inputs = document.querySelectorAll('input');
let buttonSend = document.getElementById('button-send');

//"name" parameter of input field: boolean if validated 
let inputValidator = {
  "username": false,
  "email": false,
  "password": false
}

inputs.forEach((input) => {
  input.addEventListener('input', () => {
    let name = event.target.getAttribute('name');
    let regex = new RegExp(event.target.pattern || ".*");
    //if you specified a pattern for an input field it will check if the 
    //value matches it
    //if not it will match it with ".*" which stands for everything
    if (event.target.value.length > 0 && regex.test(event.target.value)) {
      inputValidator[name] = true;
    } else {
      inputValidator[name] = false;
    };

    let allTrue = Object.keys(inputValidator).every((item) => {
      return inputValidator[item] === true
    });

    if (allTrue) {
      buttonSend.disabled = false;
    } else {
      buttonSend.disabled = true;
    }
  })
})
Comment

PREVIOUS NEXT
Code Example
Html :: navigation menu 
Html :: html error 
Html :: html ol vs ul 
Html :: call angular function from html 
Html :: how to create a button input 
Html :: srcset 
Html :: ubuntu 16.04 vmware 
Html :: embedmd 
Html :: html text next line 
Html :: footer html stack overflow 
Html :: duplicate mime type text/html nginx 
Html :: html front page 
Html :: how to add particle js in html 
Html :: What is ETH2.0? 
Html :: jokes 
Html :: span? 
Html :: i = ["hi", "he", 
Html :: how to create div with class in vs code html shortcut 
Html :: when I refresh my page it comes back to the same place html 
Html :: pre-fill the input with the default domain 
Html :: hirudhi map 
Html :: hoe to make a html page attractive 
Html :: symfony public path 
Html :: <h1 
Html :: how to make a text website 
Html :: How to create web urls without extensions 
Html :: http form 
Html :: javascript timespan 
Html :: leviathan cat bookmarks.html | grep passwd | cut -d “ “ -f9–14 
Html :: java.rmi.connectexception: connection refused to host: 127.0.0.1 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =