Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Problem with onBlur and onKeyUp

Posted on 2002-06-16
3
Medium Priority
?
1,202 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 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 2

Author Comment

by:lmred
ID: 7095956
Thanks everybody!

Lmred
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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 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…

773 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