Click here to Skip to main content
15,909,332 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HTML
<html>
<head>
<script>
	function validateForm()
	{
		var x = document.forms["myform"]["txtname"].value;
		if (x == null || x=="") {
			aleart ("please enter the name");
			return false;
		}
	}
</script>
</head>
<body>
	<form name="myform" action="demo.php">
		Name
		<input type ="text" name= "txtname"><br />
		<input type="submit" value ="Submit">
	</form>

</body>
</html>
Posted
Updated 19-Jun-15 20:40pm
v2

There is nothing php here.
First, one typo, it should be
alert 
not
aleart

Next, you have not called the validateForm() function at all
<form name="myform" action="#" onSubmit="return validateForm();">  
 
Share this answer
 
v2
During submitting your form you need to call validation function.use on submit event to call that validation function.and one more thing the alert message.its alert in stead of aleart.you can do validation simply by using Id .

HTML
<html>
<head>
<script>
	function validateForm()
	{
		var x = document.forms["myform"]["txtname"].value;
		if (x == null || x=="") {
			alert ("please enter the name");
			return false;
		}
	}
</script>
</head>
<body>
	<form name="myform" action="demo.php" onSubmit="return validateForm();">
		Name
		<input type ="text" name= "txtname"><br />
		<input type="submit" value ="Submit">
	</form>
 
</body>
</html>
 
Share this answer
 
v3
here write <form name="myform" action="demo.php" onSubmit="return validateForm();">
 
Share this answer
 
Or You can do iit by using id also
HTML
<html>
<head>
<script>
	function validateForm()
	{
		var x = document.getElementById("txtname").value;
		if (x == null || x=="") {
			alert ("please enter the name");
			return false;
		}
	}
</script>
</head>
<body>
	<form name="myform" action="demo.php" onSubmit="return validateForm();">
		Name
		<input type ="text" name= "txtname" id="txtname"><br />
		<input type="submit" value ="Submit">
	</form>
 
</body>
</html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900