Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Problem with onBlur and onKeyUp

Posted on 2002-06-16
3
Medium Priority
?
1,199 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 3

Accepted Solution

by:
davlun20080 earned 60 total points
ID: 7090693
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
ID: 7091637
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
ID: 7095956
Thanks everybody!

Lmred
0

Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

715 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