Page 1 of 1

numbers in validation script

Posted: Tue Jan 11, 2011 11:26 am
by IGGt
I Have a form which I use to enter hours and minutes onto a page (configChange.php):

Code: Select all

<form name="configChange" action="configUpdate.php" method="post" onSubmit="return ValidateForm()">

//email address section added here, which works fine //

CONFIG:</br>
	first warning after:
	<?php
		print "<select name=\"timesmallH\">";
			for($a = 0; $a <13; $a++) {
				print "<option value=$a>$a</option>";
			}
		print "</select> Hours";
		print "<select name=\"timesmallM\">";
			for($a = 0; $a <61; $a++) {
				print "<option value=$a>$a</option>";
			};
		print "</select> Minutes";
	?>
	</br>
	Second warning after:
	<?php
		print "<select name=\"timelargeH\">";
		print "<option Selected=\"selected\" value=\"$timelargeH\">$timelargeH </option>";
			for($a = 0; $a <13; $a++) {
				print "<option value=$a>$a</option>";
			}
		print "</select> Hours";
		print "<select name=\"timelargeM\">";
		print "<option Selected=\"selected\" value=\"$timelargeM\">$timelargeM </option>";
			for($a = 0; $a <61; $a++) {
				print "<option value=$a>$a</option>";
			};
		print "</select> Minutes";
	?>					
		</br>
		</br>				
		<input type="Submit" value="Save Changes"/>
		</form>
This gives me four boxes for hours and minutes (twice).

I then want to write some javascript to check that the first one is less than the second one.

I have the following javascript:

Code: Select all

function ValidateForm(){
	
	var emailAddress=document.configChange.emailAddress
	
	var timesmallH=document.configChange.timesmallH
	var timesmallM=document.configChange.timesmallM
	var timelargeH=document.configChange.timelargeH
	var timelargeM=document.configChange.timelargeM	
	
	var timesmallSEC = ((timesmallH * 60 * 60) + (timesmallM * 60))
	var timelargeSEC = ((timelargeH * 60 * 60) + (timelargeM * 60))	


if ((emailAddress.value==null)||(emailAddress.value=="")){
		alert("Please Enter an Email Address")
		emailAddress.focus()
		return false
	}

	
if (timesmallSEC.value > timelargeSEC.value){
		alert("warning text goes here")
		timesmallH.focus()
		return false
	}
	
	return true
}
if you Ignore the section about an email address as that works fine, But the other sectin just fails. I tried putting the 'timesmallSEC' and timelargeSEC' variables into the warning to see what numbers were actually being retunrned and I just get 'NaN' which is apparenlty not a number. But I can't see why this would be, as surely the form should be generating a number.

Re: numbers in validation script

Posted: Thu Jan 13, 2011 8:14 pm
by danwguy
Might not be exactly what you are looking for but I used this code and it worked fine...

Code: Select all

<?php
$min1 = $_POST['timesmallH'];
$min2 = $_POST['timesmallM'];
$min3 = $_POST['timelargeH'];
$min4 = $_POST['timelargeM'];
if($min1 > $min3 && $min2 > $min4) {
echo "<script language='javascript'>alert(\"please make sure alarm 1 is less than alarm 2\")</script>";
echo "Please go <a href='javascript:history.go(-1)'>back</a> and fix the problem";
}else{
echo "first alarm in " . $min1 . " Hours and " . $min2 . " Minutes<br />";
echo "Second alarm in " . $min3 . " Hours and " . $min4 . " Minutes<br />";
echo "Works for me.";
}
?>
That is the configUpdate.php. I know you are looking for instant validation before sending but this script I ran and it worked fine.