function checkBirthday(obj) {
if( !/^(\d{1,2})(\d{1,2})(\d{4})$/.test(obj.value) ) {
alert( "Invalid date supplied - must be format DDMMYYYY" );
obj.focus();
return;
}
var d = new Date();
var d2 = new Date(RegExp.$3, RegExp.$2, RegExp.$1);
var submit_form;
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;
submit_form = true;
}
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();
document.form.date.select();
submit_form = false;
}
}
function confirmedok()
{
if (!(submit_form==false));
{
document.form.TK.value=document.form.TK.value;
check(document.form);
}
}
I keep getting the error 'submit_form' is undefined
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
If you declare a variable with a var keyword inside a function, it is a local variable. Meaning, only the function can access it. If you want other functions to access it, make it global by declaring it outside the function.
.....
var submit_form=true;
....
var submit_form=false;
.....
function confirmedok()
{
if (submit_form==true);
{
document.form.TK.value=document.form.TK.value;
check(document.form);
}
}
This successfully sets the variable to true/false. But i still get the error?
Can i not reference a variable from another function in another function?
Heres my page:
Code:
<script language="javascript">
function checkBirthday(obj) {
if( !/^(\d{1,2})(\d{1,2})(\d{4})$/.test(obj.value) ) {
alert( "Invalid date supplied - must be format DDMMYYYY" );
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" );
var infant=true;
var submit_form=true;
alert(submit_form);
}
else if( diff > 2 )
{
alert( "This is NOT an INFANT \n they are older that 2" );
var infant=false;
var submit_form=false;
alert(submit_form);
}
}
function confirmedok()
{
alert ("confirmedok");
if (submit_form==true);
{
document.form.TK.value=document.form.TK.value;
check(document.form);
}
}
</script>
<img src="images/confirm.gif" onclick="javascript: checkBirthday(document.form.birthday); confirmedok();">