Show Custom Validation Message With setCustomValidity Using Javascript

With Below Script you can easily show your custom validation message with setCustomValidity Using Javascript code.



<!DOCTYPE html>
<html>
<body>
<!--- https://www.w3schools.com/js/js_validation_api.asp-->
<p>Type anything but "fun":</p>

<input id="id1" type="text" required>
<button onclick="myFunction()">OK</button>


<p id="demo"></p>

<script>
function myFunction() {
    var inpObj = document.getElementById("id1");
    if (inpObj.value == "fun") {
        inpObj.setCustomValidity("You're having too much fun!");
    } else {
        inpObj.setCustomValidity("You're having no fun!");
    } 
    var myresult = document.getElementById("demo")
    myresult.innerHTML = inpObj.validationMessage;
} 
</script>
</body>
</html>

Post a Comment

0 Comments