0

Hi,

Am new to javascript, i have two fields

1. Start time ( hh:mm:ss Am/Pm )
2. End time (hh:mm:ss Am/Pm)

i want to validate it. Start time should be always greater than end time.

Eg: start time : 10:00:00 Am
end time : 09:00:00 Am

It should alert the user, invalid time.


Suggesst me what to do..

3
Contributors
4
Replies
9
Views
6 Years
Discussion Span
Last Post by anandhikrishnan
0
<html>


<script language="javascript">

	function validate(dt1,dt2)
	{
		
		var jdt1=Date.parse('20 Aug 2000 '+dt1);
		var jdt2=Date.parse('20 Aug 2000 '+dt2);
		
	/*	if(jdt1=='NaN')
		{
			alert('invalid start time');
			return false;
		}
		if(jdt2=='NaN')
		{
			alert('invalid end time');
			return false;
		}*/
	  	if (jdt1>jdt2)
		{
			alert('start is greater');
		}
		else
		{
			alert('start is less equal');
		}
	
		
	}
	
</script>

<body>
<div>


<form name=frm id=frm action=temp.php method="get" accept="text/csv">
<P>&nbsp;</P>
start <INPUT id=txt1 name=txt1 value='10:30:05 am'>&nbsp; <br>
end <INPUT id=txt2 name=txt2  value='10:40:05 am'>&nbsp; 
<input type=button value=ok id=button2 name=button2 onclick ='validate(document.frm.txt1.value,document.frm.txt2.value)'>&nbsp;&nbsp; 
 </P>
</form></div>
<script language='javascript'>
</script>
<div id="txtHint">&nbsp;
</div>
<P></P>

</body>
</html>
0

" Start time should be always greater than end time. "

Cool,

finish the task before you start it

no wonder the op cant find logic to complete their code

0

Hi almostbob, I overlooked that sentence but this code should work in either way you
want.
anandhikrishnan, here is final code

<html>


<script language="javascript">

	function validate(dt1,dt2)
	{
		
		var jdt1=Date.parse('20 Aug 2000 '+dt1);
		var jdt2=Date.parse('20 Aug 2000 '+dt2);
		
		if(isNaN(jdt1))
		{
			alert('invalid start time');
			return false;
		}
		if(isNaN(jdt2))
		{
			alert('invalid end time');
			return false;
		}
	  	if (jdt1>jdt2)
		{
			alert('start is greater');
		}
		else
		{
			alert('start is less equal');
		}
	
		
	}
	
</script>

<body>
<div>


<form name=frm id=frm action=temp.php method="get" accept="text/csv">
<P>&nbsp;</P>
start <INPUT id=txt1 name=txt1 value='10:30:05 am'>&nbsp; <br>
end <INPUT id=txt2 name=txt2  value='10:40:05 am'>&nbsp; 
<input type=button value=ok id=button2 name=button2 onclick ='validate(document.frm.txt1.value,document.frm.txt2.value)'>&nbsp;&nbsp; 
 </P>
</form></div>
<script language='javascript'>
</script>
<div id="txtHint">&nbsp;
</div>
<P></P>

</body>
</html>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.