31 March 2017

How To Validate User Input Using Native JavaScript

In the presence of numerous JS frameworks and 'drag n drop' features, I just found that some graduating students of computer science can't write a simple user input validation using a pure JavaScript. What even bothered me was that some web developers can't even write a simple JS script without searching from Google or without using the so-called 'copy-paste programming'!

Okay, I'll just share with you a very simple and neat way to validate a required text entry using our native JavaScript:

Name: <input type="text" id="txtName"  /><br/>
<input type="button" id="btnSubmit"  onclick ="javascript:validate()"  value="Submit" />    

<script type="text/javascript">
        function validate()
           if (document.getElementById("txtName").value=="")
                 alert("Name is required.");
                 return false;
           return true;

No comments:

Post a Comment