...

View Full Version : Resolved Validating birthdays on forms



PumpkinPie76
12-19-2010, 08:13 AM
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.



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!:)

Philip M
12-19-2010, 08:35 AM
Do it like this:-


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:-


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

PumpkinPie76
12-19-2010, 09:21 AM
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!!

Philip M
12-19-2010, 09:27 AM
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.


<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>

PumpkinPie76
12-19-2010, 10:50 AM
would anyone happen to know what I can add to capture leap years?

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;
}
}

Philip M
12-19-2010, 12:10 PM
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:-



<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>

PumpkinPie76
12-19-2010, 12:16 PM
oh. hahaha sigh i need to rest my eyes.. thanks again!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum