Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old August 28th, 2008, 02:24 PM
Claus Mygind's Avatar
Member
 
Join Date: Mar 2008
Posts: 79
Default Preventing a keystroke value from being added to string.

with the "onKeyPress" event handler I see that I can capture and examine the value of a keystroke before it is displayed on the screen. I have made good use of this for single character fields where I want to examine if the value is correct and also convert the value to upper case like so:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <input 
  3.    type="text" 
  4.    name="f1" 
  5.    id  ="f1"  
  6.    maxLength="1"
  7.    onKeyPress="return isHorS(event);" 
  8.  />
  9.  
  10. function isHorS(evt)
  11. {
  12.    evt = (evt) ? evt : event; 
  13.    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));   
  14.  
  15.       if (charCode != 8 && charCode != 9 && charCode != 32 && charCode != 72 && charCode != 104 && charCode != 83 && charCode != 115 )
  16.       {  
  17.             alert("You must either leave blank or enter H or S!"); 
  18.              document.getElementById("f1").value = "";
  19.       }else{
  20.           if (charCode == 104  || charCode == 115 )
  21.           {
  22.             var cUp = String.fromCharCode(charCode)
  23.             document.getElementById("f1").value = cUp.toUpperCase();
  24.           }
  25.       }
  26.  
  27. }
  28.  
As I said this works great when I am only dealing with one character. But for longer strings than one character, I would like to prevent unwanted characters from becoming part of the string. Can I use this technique by intercepting what the user is typing give them an alert and then cancel that keystroke entry?
Reply
  #2  
Old August 28th, 2008, 02:57 PM
Familiar Sight
 
Join Date: Feb 2007
Posts: 207
Default

It's much simpler to use a regular expression to remove unwanted characters from the string after each keystroke. Use the onkeyup event.
Reply
  #3  
Old August 28th, 2008, 03:07 PM
Claus Mygind's Avatar
Member
 
Join Date: Mar 2008
Posts: 79
Default

great do you have an example I can use. although I did get it to work like this:
Expand|Select|Wrap|Line Numbers
  1.  onKeyPress=" return isValidCode(event, this);"
  2.  
  3. function isValidCode(evt, obj)
  4. {
  5.    evt = (evt) ? evt : event; 
  6.    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :  
  7.        ((evt.which) ? evt.which : 0));   
  8.     if ( charCode == 8 || 
  9.          charCode == 9 || 
  10.         (charCode > 47 && charCode < 58) || 
  11.         (charCode > 64 && charCode < 91) ||    
  12.         (charCode > 96 && charCode < 123) )
  13.     {
  14.         if (charCode > 96 && charCode < 123)
  15.         {
  16.             Here I would like to make it an uppercase value
  17.         }
  18.     }else{
  19.         alert("That is not a valid character ");
  20.         return false;
  21.     }
  22. }
  23.  
  24.  
Perhaps your example of a regular expression can also convert the keystroke upper case if allowed?
Reply
  #4  
Old August 28th, 2008, 03:28 PM
Claus Mygind's Avatar
Member
 
Join Date: Mar 2008
Posts: 79
Default

Ok I have learned that only IE let's you intercept a keystroke and alter it before it reaches the form (generally considered a security risk as programmer can alter what the user is entering)

Expand|Select|Wrap|Line Numbers
  1. var charCode = event.keyCode;
  2.  
  3. event.keyCode = charCode - 32
And as I am working exclusively with fireFox that will not work for me.

will look at the keyup option
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles