Keeping focus on incorrectly validated boxes

Results 1 to 2 of 2

Thread: Keeping focus on incorrectly validated boxes

  1. #1
    Rob Reid Guest

    Default Keeping focus on incorrectly validated boxes

    I am doing some form validation. On the OnChange event of each txtbox I call my function that checks the input checkInputName. This then calls the validate function which calls the relevant function for whatever type of field it is ie: PostCode, SortCode, Name, Address etc. If the validation is correct I then want to blur the current box and let the user move on to the next. If it is incorrect I want the focus to remain on the box that is incorrect, as I change all incorrect boxes blue. At the moment if I type something incorrect into a txtbox it will change it blue but then when I tab the focus goes to the next box. I won&#039;t be able to tab out of the next box but I can use my mouse to put the focus into another box. I want the focus to remain on incorrect boxes whether I tab or not. <BR>I call the function like this <BR>onChange="return top.checkInputName(this)"<BR><BR>the function where I call the setcolor and validate function is<BR><BR>function checkInputName(el) {<BR> <BR> //alert("calling checkinputname");<BR> var on = 0;<BR><BR> //var f = document.forms[0];<BR> <BR> if(validate(el)){ <BR> setColor(el, bgGood);<BR> errormessage = "";<BR> top.body.LAYOUTFORM.txtError.value = "";<BR> el.blur();<BR> return true;<BR> }else{ <BR> top.body.LAYOUTFORM.txtError.value = errormessage<BR> setColor(el, bgBad) <BR> errormessage = ""; <BR> el.focus(); <BR> return false;<BR> }<BR> <BR> <BR> }<BR><BR>help me please.

  2. #2
    Join Date
    Dec 1969

    Default Two problems...

    (1) You are using the onChange event. But when the user TABs or uses the mouse to move to another box, no change occurs! So the event code of course does not get called! You might try using onBlur instead of onChange.<BR><BR>(2) In your checkInputName code, if the validation passes you call "el.blur( );". That&#039;s a bad idea. When you "blur()" from a form field, *nothing* special gets focus. Instead, the focus is given to the window, as a whole. One consequence of this is that if the user then hits the BACKSPACE key the browser does a "BACK" to the previous page! Either don&#039;t call "el.blur()" at all and let what ever change of focus the user requested happen *or* do an explicit "otherfield.focus()" call to ensure that something other than the window gets the focus.<BR><BR>

Posting Permissions

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