Simple validation in javascript.

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.

<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 !

Related Posts

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.