<form name="form2" onSubmit="validateForm2()" action="" method="POST">
<table>
<tr>
<td> <select name="smonth"> <!--some options--> </select></td>
<td><input readonly="readonly" name="day" type="text" size="23"></td>
</tr>
</table>
</form>

<!--java script code -->
function validateForm2()
{
<!--Things to do-->
1.check if both the fields are empty
2.if condition above is true then display error

}

Recommended Answers

All 4 Replies

Depends on how you want to do it. There are many approaches to how to check. One thing you need to know is how to get your "form" element inside your JavaScript. If you use JQuery, it will be different compared to pure JavaScript. Which way are you currently using right now - JQuery or just pure JavaScript?

i am using only java script
fortunately i have solved the above problem
like this

function validateForm2b()
{
var x=document.forms["form2"]["smonth"].value;
var y=document.forms["form2"]["day"].value;
if ((x==null || x=="") && (y==null || y==""))
  {
  alert("Please Provide Search Condition!");
  return false;
  }
}

but i have a new problem.....consider the same inputs but with $i at the end of there names...$i=0 to n....depending on the number of row fetched using while loop..
so if three row are fetched the names become->
smonth0 and day0,
smonth1 and day1,
and
smonth2 and day2.
now how to check which one is empty.....

I will be happy if u guys can help me with this one
Thanks and Regards
Nadeem

hello friends readonly attribute of the input type can never be set empty so you can't leave it empty you made a mistake here and if you set it empty then you cant write it there anything so condition will always be false for the validaion i have done it in jQuery for and i will suggestion to skip to jQuery because it more easily and quick approach thanks.

<html>
<head>
<script type="text/javascript" src="js/jquery.js"></script> // include jquery.js file
<script type="text/javascript">

$(document).ready(function(){
$('#submit').click(function(){
						   
	var validation = $("#m").val();
	var validation2 = $("#text").val();
	
	if(validation == "" && validation2 == "" )
	{
	  alert("fields empty");
	}
						
});
});
</script>

</head>

<body>

<form name="form2"  action="" >
      
      <select  name="smonth" id="m">
      <option value=""></option> 
      <option value="testing">testing</option>
      <option value="testing2">testing2</option>
      </select>

      <input id="text" name="day" type="text" size="23">

      <input type="submit" id="submit" value="click me" />
   

</form>

</body>
</html>

thanks for ur reply extemer
i have solved the above problem myself, if there is a single row as mentioned earlier.
but know i have to check it for multiple rows....

Now i have a whileloop to fetch records from DB in php, like this

$i=0;
while($row = mysql_fetch_array($result))
{
echo "<select name="smonth" id="m">
<option value=""></option>
<option value="testing">testing</option>
</select>";

echo "<input readonly="readonly" id="text" name="day" type="text" size="23">";//This field contains date entered using a calander so it should be readonly
$i++;
}
echo "<input type=\"hidden\" name=\"ival\" value=\"$i\">";//used to the store number of times while loop has run

Now suppose if the loop fetch's three records the name's of input field becomes
smonth0,smonth1,smonth2 and day0,day1,day2 and the value of $i=3...

so now i need a javascript code which can run in any loop but not more then the value of $i(for this i need $i value in JS code)
and then check which field is empty...
i would like to tell you that i don't have much idea about javascript..
i have done this in php but know i need to do this using JS.
so any help would be greatly appreciated
Thanks and Regards
Nadeem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.