Hello Friends,
This is one of the simple code example for validation in javascript. By using the following code, we can make certain input field as a mandatory i.e. one cannot proceed until and unless there is certain value in the required field. In this example we use the simple login screen where we are making the field Username as mandatory. Following is the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<html> <head> <script type="text/javascript"> function validate() { var text = document.getElementById("uname").value; if (text == "") { alert('Username is mandatory'); return false; } else return true; } </script> </head> <body> <form action="#" method="POST"> Username : <input type="text" id="uname" name="uname" /><br /> Password : <input type="password" id="pwd" name="pwd" /> <br /> <input type="submit" onclick="return validate()" /> </form> </body> </html> |
Similarly you can use this validation code to validate any kind of form or page, you just have to call the function on the onclick event of submit button.
Hope this helps you.
Thanks !
Times are changing for the betetr if I can get this online!