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..
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
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")
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>
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;
}
}
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>