<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.5.2.js'></script>
</head>
<body>
<form>
<input type="text" id="email" maxlength="35" placeholder="E-mail" required>
<button type="submit">Ok</button>
</form>
<script>
$(document).ready(function(){
var elements = document.getElementsByTagName("INPUT");
for (var i = 0; i < elements.length; i++){
elements[i].oninvalid = function(e){
e.target.setCustomValidity("");
if (!e.target.validity.valid){
e.target.setCustomValidity("This field cannot be left blank");
}
};
elements[i].oninput = function(e){
e.target.setCustomValidity("");
};
}
})
</script>
</body>
</html>
HTML5 placeholder required - Change the default message
-
02-06-2022 - |
Question
I'm interested if it's possible to change the default validation message for a placeholder.
Must be a way to made a custom message.
My form:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>HTML5 Placeholder</title>
</head>
<body>
<form>
<input type="text" id="email" maxlength="35" placeholder="E-mail" required>
<button type="submit">Ok</button>
</form>
</body>
</html>
I've tried with JavaScript but the message apears everytime:
<script type="text/javascript">
document.getElementById('email').setCustomValidity('Please enter your e-mail!');
</script>
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow