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 12-19-2010, 08:13 AM   PM User | #1
PumpkinPie76
New to the CF scene

 
Join Date: Dec 2010
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
PumpkinPie76 is an unknown quantity at this point
Validating birthdays on forms

okay, my main question here is, can you nest a function in an if statement? it seems to me that you cant.. gosh validating dates is difficult.

Code:
        var userBDay = document.getElementById('BDay').value;
        var retVal = true;
	var errorMsg = "";
        if (userBDay == "")
		{
		assignErrorClass("BDay");
		errorMsg = errorMsg + "Please Enter a Birthday.\n";
		retVal = false;
		}
	if (userBDay !== "")
		{
			if (badBirthday(userBDay)) 
			{
				assignErrorClass("BDay");
				errorMsg = errorMsg + "Invalid Birthday\n";
				retVal = false;
			}
			else 
			{
				retVal = true
			}
		}
	if (!retVal)
		{
		alert( errorMsg);
		}
	return retVal;

function badBirthday (objName) 
{
	var obj = document.getElementById(objName);
	var dateStr = obj.value;
	var m = dateStr.split("/")[0];
	var d = dateStr.split("/")[1];
	var y = dateStr.split("/")[2];
	var dateObj = new Date(y,m-1,d);  //JavaScript and PHP number the months 0 to 11.
	if (dateObj.getFullYear() != y || dateObj.getMonth()+1 != m || dateObj.getDate() != d)
	 {
		return true; //if invalid bithday
	 } 
	 else 
	 {
		 return false; //if valid bithday
	 }
}

much thanks!

Last edited by PumpkinPie76; 12-19-2010 at 12:17 PM..
PumpkinPie76 is offline   Reply With Quote
Old 12-19-2010, 08:35 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Do it like this:-

Code:
if (userBDay !== "") {
var NBG = badBirthday(userBday);  // call the checking function
if (!NBG) {  // if function returns false
assignErrorClass("BDay");
errorMsg = errorMsg + "Invalid Birthday\n";
retVal = false;
}
}
Your badBirthday function should read:-

Code:
function badBirthday (passedValue) {  // here passedValue is the value of userBday
var dateStr = passedValue;
“Always acknowledge a fault. This will throw those in authority off their guard and give you an opportunity to commit more.” - Mark Twain
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
PumpkinPie76 (12-19-2010)
Old 12-19-2010, 09:21 AM   PM User | #3
PumpkinPie76
New to the CF scene

 
Join Date: Dec 2010
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
PumpkinPie76 is an unknown quantity at this point
ohhh snaps!
haha sorry. u had a good idea, but actually, functions are allowed in the if statements as long as ur not comparing(i think..). but i had a careless mistake. i put the function as badBirthdays(userBDay) instead of badBirthdays("userBDay")

but thanks!!
PumpkinPie76 is offline   Reply With Quote
Old 12-19-2010, 09:27 AM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Uh? userBay is the name of a variable to be passed. "userBday" is the literal string userBday.

Functions are of course allowed in if statements.

Code:
<script type = "text/javascript">
function first() {
var a = "Mickey";
var retval = false;
if (a == "Mickey") {
var  retval = second(a);
}
alert (retval);
}
function second(passedValue) {
if (passedValue == "Mickey") {
return true
}
else {
return false;
}
}

first();
</script>

Last edited by Philip M; 12-19-2010 at 09:35 AM..
Philip M is offline   Reply With Quote
Old 12-19-2010, 10:50 AM   PM User | #5
PumpkinPie76
New to the CF scene

 
Join Date: Dec 2010
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
PumpkinPie76 is an unknown quantity at this point
would anyone happen to know what I can add to capture leap years?
Code:
function goodBirthday(objName)
{
	var dateStr = objName;
	var m = dateStr.split("/")[0];
	var d = dateStr.split("/")[1];
	var y = dateStr.split("/")[2];
	var dateObj = new Date(y,m-1,d);  //JavaScript and PHP number the months 0 to 11.
	if (dateObj.getFullYear() != y || dateObj.getMonth()+1 != m || dateObj.getDate() != d)
	 {
		return true; //if valid birthday
	 } 
	 else
	 {
		 return false;
	 }
}
PumpkinPie76 is offline   Reply With Quote
Old 12-19-2010, 12:10 PM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Your script already deals with leap years correctly, as a simple test would have revealed. But you have got your return true and return false transposed:-


Code:
<script type = "text/javascript">

function goodBirthday() {
var dateStr = "02/29/2008";
var m = dateStr.split("/")[0];
var d = dateStr.split("/")[1];
var y = dateStr.split("/")[2];
var dateObj = new Date(y,m-1,d);  //JavaScript and PHP number the months 0 to 11.
if (dateObj.getFullYear() != y || dateObj.getMonth()+1 != m || dateObj.getDate() != d) {
alert ("Not a valid date");
return false; // if invalid birthday
} 
else {
alert ("Birthday is valid");
return true;  // if birthday is valid
}
}

goodBirthday();
</script>
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
PumpkinPie76 (12-19-2010)
Old 12-19-2010, 12:16 PM   PM User | #7
PumpkinPie76
New to the CF scene

 
Join Date: Dec 2010
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
PumpkinPie76 is an unknown quantity at this point
oh. hahaha sigh i need to rest my eyes.. thanks again!
PumpkinPie76 is offline   Reply With Quote
Reply

Bookmarks

Tags
birthdays, dates, if statement, validation

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 07:36 AM.


Advertisement
Log in to turn off these ads.