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
Solved

Problem with onBlur and onKeyUp

Posted on 2002-06-16
3
1,193 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
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Javascript/JQuery - HtmlEncode (?) Ajax Result 1 66
Make options hidden and show just option that is on 1 29
How to resize a div in html 3 38
Html value of radio 14 29
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

839 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