Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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

  • Users who have thanked Philip M for this post:

    PumpkinPie76 (12-19-2010)

  • #3
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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!!

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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.

  • #5
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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;
    	 }
    }

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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>

  • Users who have thanked Philip M for this post:

    PumpkinPie76 (12-19-2010)

  • #7
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts
    oh. hahaha sigh i need to rest my eyes.. thanks again!


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •