www.webdeveloper.com
Results 1 to 3 of 3

Thread: Strange Javascript Behavior

  1. #1
    Join Date
    Jul 2009
    Posts
    3

    Strange Javascript Behavior

    I am trying to write a CustomValidator for ASP.NET and am getting strange behavior with my javascript.

    This WORKS:
    Code:
    		var myString = "2006-07";
    
    		var mySplitResult = myString.split("-");
    		var firstyear = mySplitResult[0];
    		var firstdecade = Right(firstyear,2);
    		var secondyear = mySplitResult[1];
    		document.write("The Year Spread is " + myString);
    		document.write("<br /><br />The first year is [" + firstyear + "]");
    		document.write("<br />The first decade is [" + firstdecade + "]");
    		document.write("<br />The second year is  [" + secondyear + "]");
    		document.write("<br /><br />[first decade] + 1 = second year: " + (parseInt(firstdecade) + 1 == parseInt(secondyear)))
    
    		function Left(str, n) {
    			if (n <= 0)
    				return "";
    			else if (n > String(str).length)
    				return str;
    			else
    				return String(str).substring(0, n);
    		}
    		function Right(str, n) {
    			if (n <= 0)
    				return "";
    			else if (n > String(str).length)
    				return str;
    			else {
    				var iLen = String(str).length;
    				return String(str).substring(iLen, iLen - n);
    			}
    		}
    This DOES NOT
    Code:
    		var myString = "2007-08";
    
    		var mySplitResult = myString.split("-");
    		var firstyear = mySplitResult[0];
    		var firstdecade = Right(firstyear,2);
    		var secondyear = mySplitResult[1];
    		document.write("The Year Spread is " + myString);
    		document.write("<br /><br />The first year is [" + firstyear + "]");
    		document.write("<br />The first decade is [" + firstdecade + "]");
    		document.write("<br />The second year is  [" + secondyear + "]");
    		document.write("<br /><br />[first decade] + 1 = second year: " + (parseInt(firstdecade) + 1 == parseInt(secondyear)))
    
    		function Left(str, n) {
    			if (n <= 0)
    				return "";
    			else if (n > String(str).length)
    				return str;
    			else
    				return String(str).substring(0, n);
    		}
    		function Right(str, n) {
    			if (n <= 0)
    				return "";
    			else if (n > String(str).length)
    				return str;
    			else {
    				var iLen = String(str).length;
    				return String(str).substring(iLen, iLen - n);
    			}
    		}

    Why would 06+1=07 return TRUE
    and 07+1=08 return FALSE

  2. #2
    Join Date
    Jul 2009
    Posts
    3
    Also, this is ultimately what my function should look like... the above is a simplified version for readability.

    Code:
    //          Custom JavaScripts to enhance the user experience
    
    
    //<![CDATA[
    //FiscalYear Custom Control Client Side Validation
    function FiscalYear_ClientValidate(sender, args) {
        var textboxValue = args.Value;
    
        if (textboxValue.match(/^((1[8-9]|[2-3]\d)\d{2})$/))
            args.IsValid = true
        else if (textboxValue.match(/^((\d)(\d)(\d)(\d)-(\d)(\d))$/)) {
            var SplitYears = textboxValue.split("-");
            var FirstYear = SplitYears[0];
            var FirstYear_Decade = Right(FirstYear, 2);
            var LastYear = SplitYears[1];
    
    
            if (parseInt(FirstYear_Decade) + 1 == parseInt(LastYear)) {
                args.IsValid = true;
            }
            else if (parseInt(FirstYear_Decade) - 99 == parseInt(LastYear)) {
                args.IsValid = true;
            }
            else
                args.IsValid = false;
        }
        else
            args.IsValid = false;
        return;
    }
    //End FiscalYear Custom Control Client Side Validation
    function Left(str, n) {
        if (n <= 0)
            return "";
        else if (n > String(str).length)
            return str;
        else
            return String(str).substring(0, n);
    }
    function Right(str, n) {
        if (n <= 0)
            return "";
        else if (n > String(str).length)
            return str;
        else {
            var iLen = String(str).length;
            return String(str).substring(iLen, iLen - n);
        }
    }
    
    //]]>

  3. #3
    Join Date
    Jul 2009
    Posts
    3
    Ok, so it turns out that parseInt didn't work but parseFloat did... not entirely sure why that is.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles