Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-01-2005, 12:27 PM   PM User | #1
crmpicco
Senior Coder

 
crmpicco's Avatar
 
Join Date: Jan 2005
Location: Mauchline, Scotland
Posts: 1,091
Thanks: 15
Thanked 1 Time in 1 Post
crmpicco has a little shameless behaviour in the past
Exclamation do HTML when JavaScript variable = true

How do i only run this JavaScript validation check if a JavaScript variable = true (infant = true

Code:
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"'>
crmpicco is offline   Reply With Quote
Old 07-01-2005, 04:40 PM   PM User | #2
martin_narg
Regular Coder

 
martin_narg's Avatar
 
Join Date: Jul 2002
Location: Chamonix, France
Posts: 600
Thanks: 1
Thanked 3 Times in 3 Posts
martin_narg is an unknown quantity at this point
Code:
<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
Code:
<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
__________________
"Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
-Tommy Cooper
martin_narg is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:36 PM.


Advertisement
Log in to turn off these ads.