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 4 of 4
  1. #1
    Senior Coder crmpicco's Avatar
    Join Date
    Jan 2005
    Location
    Mauchline, Scotland
    Posts
    1,096
    Thanks
    15
    Thanked 1 Time in 1 Post

    Angry drop-down menu dates validation

    Code:
    								 			 
    <script language="javascript">
    ///////////////////////////////////////
    // Date of Birth Validation			 //
    // Check if Infant < 2 Jahre Alt	 //
    // Check if Child < 12 Jahre Alt	 //
    ///////////////////////////////////////
    Date.prototype.getDiff = function(date, interval){
      if (typeof date == "string"){
         date = new Date(date);
      }
      if (isNaN(date) || !(date instanceof Date)){
         return NaN; 
      }
      if (typeof interval == "undefined") interval = "ms"; 
      var diff = this - date; 
      switch(interval.toLowerCase()){
        case "s": 
        	diff = diff/1000; break;
        case "n": 
        	diff = diff/(1000*60); break;
       	case "h": 
    		diff = diff/(1000*60*60); break;
       	case "d": 
        	diff = diff/(1000*60*60*24); break;
        case "m": 
        	diff = diff/(1000*60*60*24*30); break;
        case "y": 
        	diff = diff/(1000*60*60*24*365); break;
        default:
          ; 
      }
      return Math.floor(diff);
    }
    
    function checkBirthday(obj) {
    	if( !/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/.test(obj.value) ) {
    		alert( "Invalid date supplied - must be format DD/MM/YYYY" );
    		obj.focus();
    		return;
    	}
    	
    	var d = new Date();
    	var d2 = new Date(RegExp.$3, RegExp.$2, RegExp.$1);
    	
    	var diff = d.getDiff(d2, "y")
    	if( isNaN(diff) ) {
    		alert( "Invalid date supplied" );
    	}
    	else if( diff < 2 ) {
    		alert( "This IS an INFANT \n they are younger that 2" );
    		infant = true;
    	}
    	else if( diff > 2 ) {
    		alert( "This is NOT an INFANT \n they are older that 2" );
    		infant = false;
    		document.form.birthday.value = "";
    		document.form.birthday.focus();
    		
    	}
    	
    }
    
    </script>
    Code:
    <select name="date<%=k%>"
    onChange="document.form.birthday.value=document.form.birthday.value+document.form.date<%=k%>.options[document.form.date<%=k%>.selectedIndex].value;"
    style="color: black; font: 7pt Verdana, Arial, Helvetica, sans-serif;">
    <%
    For d = 1 to 31
    	Response.write "<option value=" & d & ">" & d & "</option>"
    Next
    %>
    </select>
    <select name="month<%=k%>"
    onChange="document.form.birthday.value=document.form.birthday.value+document.form.month<%=k%>.options[document.form.month<%=k%>.selectedIndex].value;"
    style="color: black; font: 7pt Verdana, Arial, Helvetica, sans-serif;">
    <%
    For m = 1 to 12
    	response.Write "<option value=" & m & ">" & MMtoMMM(m) & "</option>"
    Next
    %>		
    </select>
    <% 
    Dim three_years_ago
    three_years_ago = year(date) - 3
    %>
    <select name="year<%=k%>"
    onChange="document.form.birthday.value=document.form.birthday.value+document.form.year<%=k%>.options[document.form.year<%=k%>.selectedIndex].value;"
    style="color: black; font: 7pt Verdana, Arial, Helvetica, sans-serif;">
    <%
    For y = three_years_ago to year(date)
    	Response.Write "<option value=" & y & ">" & y & "</option>"
    Next
    %>
    </select>
    
    <input type="text" name="birthday" size="5" style="font:7pt;" value="" maxLength="10"> 
    
    <img src="images/confirm.gif" alt="Confirm" name="HoldFlights" width="80" height="15" border=0  id=submit1 title="The reservations will be confirmed now" onclick="javascript: checkBirthday(document.form.birthday);return confirmedok();document.body.style.cursor = 'wait';" onMouseOver='style.cursor="hand"'  value="Confirm">
    I am having a problem with this code. I am looking to have JS validation on the drop-down menus like I said before.
    I am trying to put the values of the drop-down menus into a textbox then validate it.
    Although, i am experiancing problems. How can i do this? I am trying to check if the date of birth is MORE than two years ago
    (i.e. if the infant is older than 2years old). If they are then dont submit the form and show pop-up (ALERT). Else, submit form to next page.

  • #2
    Senior Coder crmpicco's Avatar
    Join Date
    Jan 2005
    Location
    Mauchline, Scotland
    Posts
    1,096
    Thanks
    15
    Thanked 1 Time in 1 Post

    Question

    Is there a way to put JavaScript validation onto this top prevent a date being entered that is after TODAYS Date?
    The ASP <%=k%> is just a integer value of 1,2,3 etc..
    Code:
    <input type="hidden" name="birthday<%=k%>" size="5" style="font:7pt;" maxLength="10">
    Typical Value: 07112005 for 7th November 2005

  • #3
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Hi mate, any chance you could put the credits back in for the getDiff function please. I'm not overly stressed about my own credits in other coder's code, but I didn't write that function and it would be appropriate to include that programmer.

    thanks

    m_n
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper

  • #4
    Senior Coder crmpicco's Avatar
    Join Date
    Jan 2005
    Location
    Mauchline, Scotland
    Posts
    1,096
    Thanks
    15
    Thanked 1 Time in 1 Post
    yep, i put them back


  •  

    Posting Permissions

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