Skip to content

Instantly share code, notes, and snippets.

@kobus1998
Created January 8, 2021 14:38
Show Gist options
  • Select an option

  • Save kobus1998/0d5bf8c729e5e592a7efa4ad6a9d4529 to your computer and use it in GitHub Desktop.

Select an option

Save kobus1998/0d5bf8c729e5e592a7efa4ad6a9d4529 to your computer and use it in GitHub Desktop.
create custom html5 form rules
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="/" id="form">
<input type="text" fixed="abc" id="custom">
<button>submit</button>
</form>
<script src="main.js"></script>
</body>
</html>
/*
* @see https://developer.mozilla.org/en-US/docs/Learn/Forms/Form_validation
*/
let els = document.querySelectorAll('[fixed]')
els.forEach(function (el) {
el.addEventListener('input', function (e) {
let val = this.getAttribute('fixed') // get the fixed rule
if (this.value != val) {
this.setCustomValidity('invalid fixture') // set error message (form won't submit)
} else {
this.setCustomValidity('') // reset error message
}
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment