Sample Validations in javascript

22-02-2017 validations javascript

The sample code for basic validation for textbox,radio button and drop down

the code is 

<html>
<script type="text/javascript">
function validate()
{
	var nam = document.getElementById("name").value;
	var pas = document.getElementById("pas").value;
	var rad = document.getElementById("rad");
	var sele = document.getElementById("sel").value;

	if(nam =='')
	{
		alert("Please enter name");
		return false;
	}
	
	if(pas == '')
	{
		alert("Please enter password");
		return false;
	}
	if(!rad.checked)
	{
		alert('not checked');
		return false;
	}
	
	if(sele == '')
	{
		alert("Please select");
		return false;
	}
}
</script>
<form onsubmit="return validate()">

	<input type="text" id="name" >
	<input type="password" id="pas" >
	<input type="radio" id="rad" value="m">
	<select id="sel">
		<!--<option value="default">Select</option>-->
		<option value="">Select</option>
		<option value="book">Book</option>
		<option value="pen">Pen</option>
		<option value="box">Box</option>
	</select>
<input type="submit" value="save">
</form>
</html>

 

 

You might also like:

Example for Conditional Operator in php
16-04-2017 php Conditional Operator example
How to make an http request in node js
14-03-2017 http module node js
How to get current date and time (yyyy-mm-dd H:i:s) using javascript
09-03-2017 javascript date time
Navigate to another page using java script
06-03-2017 javascript redirection navigation
Filter for change Date Time format (MM-dd-yyyy HH:mm) in Angular Js
05-03-2017 mm-dd-yyyy HH:MM date formats angular js