...

View Full Version : do HTML when JavaScript variable = true



crmpicco
07-01-2005, 01:27 PM
How do i only run this JavaScript validation check if a JavaScript variable = true (infant = true;)



function checkBirthday(obj)
{
if( !/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/.test(obj.value) ) {
alert( "Invalid date supplied - must be format DD/MM/YYYY" );
//obj.focus();
return;
}

var d = new Date();
var d2 = new Date(RegExp.$3, RegExp.$2, RegExp.$1);

var diff = d.getDiff(d2, "y")
if( isNaN(diff) ) {
alert( "Invalid date supplied" );
}
else if( diff < 2 ) {
alert( "This IS an INFANT \n they are younger that 2" );
infant = true;
document.form.submit();

}
else if( diff > 2 ) {
alert( "This is NOT an INFANT \n they are older that 2" );
infant = false;
document.form.birthday.value = "";
document.form.date.focus();
}

}
<img src="g_toolbar_flights.gif" onClick="javascript: checkRetdt(document.form.retdt);" onMouseOver='style.cursor="hand"'>

martin_narg
07-01-2005, 05:40 PM
<script>
function checkBirthday() {
if( !/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/.test(obj.value) ) {
alert( "Invalid date supplied - must be format DD/MM/YYYY" );
return false;
}

var d = new Date();
var d2 = new Date(RegExp.$3, RegExp.$2, RegExp.$1);

var diff = d.getDiff(d2, "y")

if( isNaN(diff) ) {
alert( "Invalid date supplied" );
return false;
} else if( diff < 2 ) {
return true;
} else if( diff > 2 ) {
alert( "This is NOT an INFANT \n they are older than 2" );
document.form.birthday.value = "";
document.form.date.focus();
return false;
}

}
</script>

You should really get into the habit of better naming conventions for your HTML elements, for example "frmBirthday" would be better than "form" and "txtDate" better than "date". This is for clarification in the code between JavaScript objects and HTML element ID's (ie clarification between the JavaScript Date object and date being the name of the form - only a capital D seperates the two.

We would need to see the checkRetdt() function to see what it does, but this is essentially the code if checkRetdt() returns true/false


<a href="#" onclick="if(checkBirthday()&&checkRetdt(document.form.retdt))document.form.submit();return false;"></a><img src="g_toolbar_flights.gif" border="0"></a>


(I'm sure i've written your original code in a previous post before it's been modified)

Hope this helps

m_n



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum