Solved

Problem with onBlur and onKeyUp

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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-…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…
Suggested Courses

734 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