Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2507
  • Last Modified:

onblur and onkeypress: onblur triggered on each keystroke when both events are included

I am trying to build an input validation function that will be called either when the user leaves the text field or when the user hits <enter> inside the tex field.  In order to do that, I need to have the function (or two functions) called when 2 types of events are triggered:  onblur (for when the text field loses focus) and onkeypress (where I can check if the keycode == 13, and, if it does, to try to validate; if the keycode != 13, I ignore it).

What I am discovering is that when I add the onblur event to the text field, it is only triggered when the text field loses focuses.  Similarly, if the keycode is the only event, it gets called on every keystroke.  However, if I have both events inside the <input> tag, then the onblur event is triggered BOTH on every keystroke and when the text field loses focus.  This means when the user types a keystroke, first the onblur event is triggered then the keystroke event is triggered.  (I am using IE6).

Is there any way to include both events but to have them triggered for the appropriate reasons and not to have the onblur event triggered from keystrokes?
0
msalamon
Asked:
msalamon
  • 2
  • 2
1 Solution
 
ZvonkoSystems architectCommented:
I cannot verify your observation in my IE6.0
Do you perhaps use alert() in your onkeypress handler? Then of course you have every time a blur.

Check this:

<html>
<body>
<form>
<input type=text name="Input" onBlur="window.status+='blur;'" onKeyPress="window.status+='press;'">
</form>
</body>
</html>

0
 
msalamonAuthor Commented:
Needless to say, you're a genius!  Thanks.  However...

In the situations where the text entered is not validated, i use aler() to inform the user of the type of error.  Thus, in situations where the user hits <enter> and the text is not validated, an alert is issued.  This triggers onblur, which calls the function, and then the function is called again, so the alert is shown twice. Is there any way to "smother" the triggering of onblur when alert() is called, or to detect that onblur has been called by an alert so I can ignore it, or something similar to that?
0
 
msalamonAuthor Commented:
One more thing.  When the user hits <enter>, whether it is validated or not, the text inside the text field disappears.  Any idea how to stop that?
0
 
ZvonkoSystems architectCommented:
The text does NOT disapear normaly, so its again some side effect.

Ok, your question was how to suppress the onBlur event as long as your onKeyPress is prcessing its business.
Simply like this:

<html>
<body>
<form>
<input type=text name="Input" onFocus="cB=true" onBlur="if(cB)alert('blur')" onKeyPress="cB=false;kC=event.keyCode;alert(kC);cB=true;if(kC==13){this.blur();return false;}">
</form>
</body>
</html>

0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now