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 3 of 3
  1. #1
    New Coder
    Join Date
    May 2004
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Date Validation, HELP!!

    I have 2 scripts on my page. One that validates dates and the other validates the form's text fields:

    Code:
    /* Validate Date Script*/
    var dtCh= "-";
    var minYear=1900;
    var maxYear=2100;
    
    function isInteger(s){
    	var i;
        for (i = 0; i < s.length; i++){   
            // Check that current character is number.
            var c = s.charAt(i);
            if (((c < "0") || (c > "9"))) return false;
        }
        // All characters are numbers.
        return true;
    }
    
    function stripCharsInBag(s, bag){
    	var i;
        var returnString = "";
        // Search through string's characters one by one.
        // If character is not in bag, append to returnString.
        for (i = 0; i < s.length; i++){   
            var c = s.charAt(i);
            if (bag.indexOf(c) == -1) returnString += c;
        }
        return returnString;
    }
    
    function daysInFebruary (year){
    	// February has 29 days in any year evenly divisible by four,
        // EXCEPT for centurial years which are not also divisible by 400.
        return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
    }
    function DaysArray(n) {
    	for (var i = 1; i <= n; i++) {
    		this[i] = 31
    		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
    		if (i==2) {this[i] = 29}
       } 
       return this
    }
    
    function isDate(dtStr){
    	var daysInMonth = DaysArray(12)
    	var pos1=dtStr.indexOf(dtCh)
    	var pos2=dtStr.indexOf(dtCh,pos1+1)
    	var strYear=dtStr.substring(0,pos1)
    	var strMonth=dtStr.substring(pos1+1,pos2)
    	var strDay=dtStr.substring(pos2+1)
    	strYr=strYear
    	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
    	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
    	for (var i = 1; i <= 3; i++) {
    		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
    	}
    	month=parseInt(strMonth)
    	day=parseInt(strDay)
    	year=parseInt(strYr)
    	if (pos1==-1 || pos2==-1){
    		alert("Please enter a date in the following format: YYYY-MM-DD")
    		return false
    	}
    	if (strMonth.length<1 || month<1 || month>12){
    		alert("Please enter a valid 2 digit month ie. 01 = January, 12 = December")
    		return false
    	}
    	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
    		alert("Please enter a valid day of the month")
    		return false
    	}
    	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
    		alert("Please enter a valid 4 digit year")
    		return false
    	}
    	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
    		alert("Please enter a valid date")
    		return false
    	}
    return true
    }
    Code:
    /* Validate Form Script */
    function ValidateForm(){
    	var dt1=document.IMCoutageForm.plannedDate
    	var dt2=document.IMCoutageForm.estrestoreDate
    	var dt3=document.IMCoutageForm.dispatchDate
    	var dt4=document.IMCoutageForm.restoreDate
    	var time1=document.IMCoutageForm.plannedTime
    	var time2=document.IMCoutageForm.estrestoreTime
    	var time3=document.IMCoutageForm.dispatchTime
    	var time4=document.IMCoutageForm.restoreTime
    
    	// Validate Planned Date/Time
    	if (dt1.value != "" || time1.value != "")
    	{
    		if (isDate(dt1.value)==false){
    			dt1.focus();
    			return false;
    		}
    		
    		if (IsValidTime(time1.value)==false){
    			time1.focus();
    			return false;
    		}
    	}	
    	
    	// Validate Estimated Restore Date/Time
    	if (dt2.value != "" || time2.value != "")
    	{
    		if (isDate(dt2.value)==false){
    			dt2.focus();
    			return false;
    		}
    		
    		if (IsValidTime(time2.value)==false){
    			time2.focus();
    			return false;
    		}
    	}
    	
    	// Validate Dispatched Date/Time
    	if (dt3.value != "" || time3.value != "")
    	{
    		if (isDate(dt3.value)==false){
    			dt3.focus();
    			return false;
    		}
    		
    		if (IsValidTime(time3.value)==false){
    			time3.focus();
    			return false;
    		}
    	}
    	
    	// Validate Restored Date/Time
    	if (dt4.value != "" || time4.value != "")
    	{
    		if (isDate(dt4.value)==false){
    			dt4.focus();
    			return false;
    		}
    		
    		if (IsValidTime(time4.value)==false){
    			time4.focus();
    			return false;
    		}
    	}
    	
    	return true;
    }
    With the help of my current scripts, can anyone tell me how I might add a function that checks to see if the month and day between 1 and 9 have a 0 in front of them when the date is entered into the text fields listed above (plannedDate, estrestoreDate, dispatchDate, restoreDate). I either want it to automatically enter the zero if they forget or have a pop-up box remind them too, right now it leaves the month and day as: january 1st 2004 as 2004-1-1.
    Thanks for any help.

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <script type="text/javascript">
      function format(number){
        var zero="";
        if(number<10 && number!=0) zero+="0";
        return zero+number.toString();
      }
    </script>
    
    <form>
    <input type="text" onchange="this.value=format(this.value)">
    </form>

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I use lines like this to enforce such things


    var month='00'.substring(0, 2-(''+month).length)+month;


  •  

    Posting Permissions

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