Solved

Problem with onBlur and onKeyUp

Posted on 2002-06-16
3
1,189 Views
Last Modified: 2008-07-03
I am validating characters. I am sending the control to a function when it is onBlur and when it is onKeyUp. I need an error message to display if the character is entered....or if the user tries to leave the control with an invalid character. But if I use both of these events...I get 2 error messages instead of one. Even when the user hasn't left the control yet. How can I prevent this? Here is my code:

function ValidateChar(control)
{
//***this function checks for invalid characters
var CharsNotAllowed = viewsid.txtCharsNotAllowed.value;
var ArrayCharsNotAllowed = new Array();
var varField;
var varSearch;
var varReplace = '"';
var sVal = "";
//***get characters not allowed from hidden variable
ArrayCharsNotAllowed = CharsNotAllowed.split("$$");
               
for (var x=0; x < ArrayCharsNotAllowed.length; x++)
{
               
varField = ArrayCharsNotAllowed[x].split("**");
               
if ("txt" + varField[0] == control.name)
     {
     sVal=control.value;
     var varChar;
     for (var i=0; i < sVal.length; i++)
     {
     varChar = sVal.charAt(i);    
         
      varField[1].replace("''",varReplace);
                                        // alert("The value is " + varField[1]);
                                        if (varField[1].indexOf(varChar) != -1)
     {
     alert('The character ' + varChar + ' is not allowed.');
                                        control.focus();
                                   control.select();
                                   return true;
                                }
                                   
                         }
                    }
               }
               //return false;
          }
0
Comment
Question by:lmred
3 Comments
 
LVL 3

Accepted Solution

by:
davlun20080 earned 20 total points
Comment Utility
Maybe i misunderstand how the onkeyup works, but it is my thinking that onkeyup, will always fire on the form element when the user lifts up the key unless the user some how manages to move the focus of the browser to another element WHILE the key is still held down.

In this case, wouldn't the onkeyup event be sufficient to catch the invalid characters?  You can return focus to the field in script if necessary.
0
 

Expert Comment

by:rasaraja
Comment Utility
Hi Imred,
  Although I didn't go through the full code, but for input validations, the onKeyUp event is the best, it fires whenever the User releases the key. The advantage is whenever the Key is released the value of that key is available in the Value attribute of the field.
   It's not like onKeyDown event which fires before the value of the key is available in the input box.

So u can safely test the value of the input control with only onKeyUp event.

The onBlur event is not at all needed.
0
 
LVL 1

Author Comment

by:lmred
Comment Utility
Thanks everybody!

Lmred
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now