Connecting Tech Pros Worldwide Forums | Help | Site Map

Preventing a keystroke value from being added to string.

Claus Mygind's Avatar
Familiar Sight
 
Join Date: Mar 2008
Posts: 173
#1: Aug 28 '08
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?

Familiar Sight
 
Join Date: Feb 2007
Posts: 207
#2: Aug 28 '08

re: Preventing a keystroke value from being added to string.


It's much simpler to use a regular expression to remove unwanted characters from the string after each keystroke. Use the onkeyup event.
Claus Mygind's Avatar
Familiar Sight
 
Join Date: Mar 2008
Posts: 173
#3: Aug 28 '08

re: Preventing a keystroke value from being added to string.


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?
Claus Mygind's Avatar
Familiar Sight
 
Join Date: Mar 2008
Posts: 173
#4: Aug 28 '08

re: Preventing a keystroke value from being added to string.


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