Link to home
Start Free TrialLog in
Avatar of Kathrynmj
Kathrynmj

asked on

Focus on empty or error field will not "stick".

In the verify code I have:
 if (!(fFieldsChk[iLoop])&& (Ctrim == "") )  //check for empty field
            {
                 alert (strField + " is null. " + "\n" + "Please complete the " + strField + " field.");
                  
//-------------------------------------------------------------------------------------------
// This is the area that checks for empty fields on the input form
// iLoop is an index to the elements of the form
//-------------------------------------------------------------------------------------------
            if (iLoop  ==  0)
                  { return false; document.theForm.NewCourseID.focus(); }
            if (iLoop  ==  1)
                  { return false; document.theForm.CourseName.focus(); }
            if (iLoop  ==  2)
                  { return false; document.theForm.CourseDesc.focus(); }
.
.
.
break;                  
            }                        // end of if (!(fFieldsChk[iLoop])&& (Ctrim == "") )  
FORM:
<FORM METHOD="post" NAME="theForm" onsubmit="return verify(this)">
<table border="0" width="400" cellpadding="8" cellspacing="0" >
<tr >
      <td ><b>Course ID:  </b></td>
      <td colSpan="2" rowSpan="1" ><INPUT maxlength="6" size="6" name="NewCourseID" colSpan="2"value=""></td></tr>
<tr>
      <td ><b>CourseName:</b></td>
      <%      If  ReEnterFlag = true Then      %>
                  <td colspan="2"><INPUT name="CourseName" maxlength="35" value="<% Response.Write SaveInput(2) %>"></input></td></tr>
            <%      Else      %>
                        <td colspan="2"><input name="CourseName" maxlength="35"  value=" "></td></tr>
            <%      End If      %>
</tr>
<tr>
      <td ><b>Course Desc:  </b></td>
            <%      If  ReEnterFlag = true Then      %>
                        <td colspan="2"><TEXTAREA name="CourseDesc" maxlength="1200" rows= "2" cols="65" wrap="wrap" ><% Response.Write SaveInput(3)%></TEXTAREA></td></tr>
            <%      Else      %>
                        <td colspan="2"><TEXTAREA name="CourseDesc" maxlength="1200" rows= "2" cols="65" ></TEXTAREA></td></tr>
            <%      End If      %>      
</tr>
.
.
.
When I have an empty field onsubmit finds it, the cursor goes into the field for less than 2 seconds, and then proceeds with the execution.  Does anyone see what is wrong?  Thanks a lot
Avatar of hielo
hielo
Flag of Wallis and Futuna image

First of all, you need to focus first then return false:
WRONG=> { return false; document.theForm.CourseDesc.focus(); }
Secondly, your issue must lie in the code you are not displaying. You posted a fragment of your code. On that fragment it clearly shows under what conditions false is being returned. What it does not show is when/where are you returning true. Your form will not submit if you return false. Hence, the problem is elsewhere.
all the if blocks should be like  

{document.theForm.NewCourseID.focus();  return false; }

other than that i dont see any problems
<fyi>When javascript encounters a "return" command, it returns, period. Nothing further in that function will be executed</fyi>
Avatar of Kathrynmj
Kathrynmj

ASKER

I changed the returns to the correct place and reran this.  I still did not get the focus.  Any suggestions?? Thanks
Program1.txt
ASKER CERTIFIED SOLUTION
Avatar of Lakio
Lakio
Flag of Iceland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I went to FireFox and tried my page, and it worked!!!!  I have to look into the problem in IE...
you can turn on "Display a notification about every script error" in IE

Go to Internet Options --> Browsing -->  "Display a notification about every script error"
check that box and restart IE and try your form. You will most get an error there then =P