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

    Exclamation do HTML when JavaScript variable = true

    How do i only run this JavaScript validation check if a JavaScript variable = true (infant = true

    Code:
    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;
    		document.form.submit();
    		
    	}
    	else if( diff > 2 ) {
    		alert( "This is NOT an INFANT \n they are older that 2" );
    		infant = false;
    		document.form.birthday.value = "";
    		document.form.date.focus();
    	}
    	
    }
    <img src="g_toolbar_flights.gif" onClick="javascript: checkRetdt(document.form.retdt);" onMouseOver='style.cursor="hand"'>

  • #2
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Code:
    <script>
    function checkBirthday() {
    	if( !/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/.test(obj.value) ) {
    		alert( "Invalid date supplied - must be format DD/MM/YYYY" );
    		return false;
    	}
    	
    	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" );
    		return false;
    	} else if( diff < 2 ) {
    		return true;
    	} else if( diff > 2 ) {
    		alert( "This is NOT an INFANT \n they are older than 2" );
    		document.form.birthday.value = "";
    		document.form.date.focus();
    		return false;
    	}
    	
    }
    </script>
    You should really get into the habit of better naming conventions for your HTML elements, for example "frmBirthday" would be better than "form" and "txtDate" better than "date". This is for clarification in the code between JavaScript objects and HTML element ID's (ie clarification between the JavaScript Date object and date being the name of the form - only a capital D seperates the two.

    We would need to see the checkRetdt() function to see what it does, but this is essentially the code if checkRetdt() returns true/false
    Code:
    <a href="#" onclick="if(checkBirthday()&&checkRetdt(document.form.retdt))document.form.submit();return false;"></a><img src="g_toolbar_flights.gif" border="0"></a>
    (I'm sure i've written your original code in a previous post before it's been modified)

    Hope this helps

    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


  •  

    Posting Permissions

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