How to change focus?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sreemati
    New Member
    • Jun 2007
    • 42

    How to change focus?

    Hi

    One of the web form has 3 dates fields: Date of form entry, date of diagnosis and date of death.

    Date of form entry has future date validation done which I thought of extending to the other two date fields.

    Code being used already:

    Code:
    	function checkFutureDate(day, month, year, formName)
    	{
    		if (year > <%=zYear%>)
    		{
    			alert("Year cannot be later than the current year.");
    			document.frmUser.date3.focus();
    			return false;
    		}
    		
    		else if ((month > <%=zMonth%>) && (year == <%=zYear%>))
    		{
    			alert("Month cannot be later than the current month.");
    			document.frmUser.date2.focus();
    			return false;
    		}	
    		
    		else if ((day > <%=zDay%>) && (month == <%=zMonth%>) && (year == <%=zYear%>))
    		{
    			alert("Day cannot be later than the current day.");
    			document.frmUser.date1.focus();
    			return false;
    		}
    	
    		return true;
    	}
    
    its been called from another function:
    
    var ok = checkFutureDate(document.frmUser.date1.value, document.frmUser.date2.value, document.frmUser.date3.value);
    		
    		if(!ok)
    			return false;
    I just thought I will use the same code with my date values:

    Code:
    var ok = checkFutureDate(diagdate1, diagdate2, diagdate3);
    	if(!ok)
    			return false;
    			
    			var ok = checkFutureDate(death1, death2,death3); 
    		
    		if(!ok)
    			return false;
    It validate fine but the error message is vague and also the focus just returns to the date of form entry: date3 / date 2 or date1

    Help I need?

    Please tell me how can I provide a customized error message such that if the future date value if for date of entry it says: Your day value for date of entry (Similarly replace it with date of diagnosis/ date of death as applicable) can not be later than today's day.

    Secondly, to return the focus, to the exact field that is wrong.

    Can I pass the field values as variables and pass them instead of a fixed date1, date2 or date3 in the function that is validating for future value?

    Hope I am clear, otherwise please let me know.

    Thanks for your help in advance,
    Sree
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    Hey Sreemati, could you please explain it properly?..

    I could not understand what's not working, and what do you want to achieve.In function checkFutureDate , there is an argument formName, which is never used. And when it finds an invalid entry, it returns focus of the date1,2,3 accordingly. So where is the problem?

    Regards,
    Harpreet

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Rather than pass values, pass a reference to the text field objects to the function. This will allow you to change focus to those elements when there's an error. The values are easy to retrieve using the value property. Change the focus() statements to use the arguments of the function. If you have trouble implementing this, post your attempt here.

      Comment

      Working...