Skip to content

Instantly share code, notes, and snippets.

@jdanyow
Created September 11, 2019 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdanyow/c380b72f801d7be35fa2d68cf4a6b06d to your computer and use it in GitHub Desktop.
Save jdanyow/c380b72f801d7be35fa2d68cf4a6b06d to your computer and use it in GitHub Desktop.
customValidity property
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>GistRun</title>
</head>
<body>
<h1>Hello world!</h1>
<form action="javascript:alert('submitted')">
<fieldset>
<legend>Choose at least one of these</legend>
<label><input type="checkbox" id="firstinput"> apples</label>
<label><input type="checkbox"> oranges</label>
<label><input type="checkbox"> pineapple on pizza</label>
<label><input type="checkbox"> flavor country</label>
</fieldset>
<button type="submit">Submit</button>
<button type="button" onclick="javascript:firstinput.customValidity = ''">Clear custom validity</button>
</form>
<script>
// expose a customValidity property for lit binding
Object.defineProperty(HTMLInputElement.prototype, 'customValidity', {
set(value) {
this.setCustomValidity(value);
}
});
</script>
<script>
firstinput.customValidity = 'You must check at least one of the checkboxes';
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment