Letter Validation in JavaScript

There will be some scenarious where you want users to enter only letters like (A-Z or a-z), We can do letter only validations using JavaScript.

<html>
<body>
<script type='text/javascript'>
function letterValidation()
{
var letterValue = /^[a-zA-Z]+$/;
if(document.getElementById('txtletter').value.match(letterValue))
{
alert("Yes you enter letters only.");
return true;
}
else
{
alert('Enter only Letters like A-Z or a-z');
txtletter.focus();
return false;
}
}
</script>
<form>
Please Letters Only: <input type='text' id='txtletter'/>
<input type='button' id="btnSubmit" onclick="letterValidation()" value='Submit' />
</form>
</body>
</html>

Leave a Reply

Your email address will not be published.