Click here to Skip to main content
15,922,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I Want To Ask That On Submit, If Textbox In Myparents Empty It Will Alert Myparents,If Textbox In Yourparents Empty It Will Alert Your Parents. I Want To Do It With Javascript. How Can I Do This?

HTML
<html>
<head><title></title>
</head>
<body>
<form method="post" action="" onsubmit="check()">
    <div class="myparents">
        <input type="text" class="validate[required]" />
    </div>
    <div class="yourparents">
        <input type="text" class="validate[required]" />
    </div>
    <input type="submit" value="submit" />
</form>
</body>
</html>
Posted
Updated 13-Mar-14 16:12pm
v2

This was my solution...but this is probably better
JavaScript Form Validation[^]
JavaScript
function check(){
		var valid true;
		var in1 = document.getElementById("var1");
		var in2 = document.getElementById("var2");
		if(undefined == in1.value || undefined == in2.value)
			valid = false;
			
		if(valid){
			var val1 = in1.value;
			var val2 = in2.value;
			if(val1.length==0 || val2.length == 0){
				valid = false;
			}
		}
		
		if(!valid){
			alert("All fields required!");
		}
	}
 
Share this answer
 
Let do it together:
1. in the form tag, modify the onsubmit event, when the return is false, the submit will be cancelled
<form method="post" action="" onsubmit="return check()">

2. add ids to the input texts for myparents and yourparents, so that we can access them in javascript function later on:
<input type="text" id="myparents" class="validate[required]" />

<input type="text" id="yourparents" class="validate[required]" />

3. Now, write the check() function:
XML
<script type="text/javascript">
function check()
{
   var myparents = document.getElementById("myparents").value;
   var yourparents = document.getElementById("yourparents").value;

   var status = true;
   if (!myparents.trim()) {
      alert("My parents cannot be blank!");
      status = false;
   }
   if (!yourparents.trim()) {
      alert("Your parents cannot be blank!");
      status = false;
   }
   return status;
}
</script>

The End.
 
Share this answer
 
v6

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