JavaScript Validation Examples

We can validate input controls using JavaScript easily. Below are a few JavaScript validation examples.

Textbox required validation in javascript

By using JavaScript, we can validate for empty value in textboxes. Whenever an user tries to submit a form without entering a value in the textbox, it will display mandatory field validation messages in an alert box.

<html>
<body>
<script type='text/javascript'>
function textBoxValidation()
{
if(document.getElementById('txtBox').value.trim()== "")
{
alert('Textbox should not be blank');
txtBox.focus();
return false;
}
alert("Your name submitted sucessfully")
return true;
}
</script>
<form>
Name:<input type='text' id='txtBox'/> <input type='button' onclick="textBoxValidation()"id="btnSubmit" value='Submit' />
</form>
</body>
</html>

Leave a Reply

Your email address will not be published.