<form method="post">
<input type="text" name="cap"
autocomplete="off" required="required" oninvalid="setCustomValidity('googoli')" />
<input type="submit" />
oninput="check(this)"
<script>
function check(input){
if(input.value=='ok'){
input.setCustomValidity('');
} else {
input.setCustomValidity('error!!');
}
}
</script>
<!DOCTYPE html>
<html>
<script>
function check(input){
if(input.value){
input.setCustomValidity('');
} else {
input.setCustomValidity('error!!');
}
}
</script>
<body>
<form>
<input type="text" oninput="check(this)" required
oninvalid="setCustomValidity('Custom Message')" />
<input type="submit" value="send" />
</form>
</body>
</html>