Created
September 11, 2019 18:33
-
-
Save jdanyow/c380b72f801d7be35fa2d68cf4a6b06d to your computer and use it in GitHub Desktop.
customValidity property
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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