Preventing a keystroke value from being added to string.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    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:

    Code:
      
    <input 
       type="text" 
       name="f1" 
       id  ="f1"  
       maxLength="1"
       onKeyPress="return isHorS(event);" 
     />
    
    function isHorS(evt)
    {
       evt = (evt) ? evt : event; 
       var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));   
    
          if (charCode != 8 && charCode != 9 && charCode != 32 && charCode != 72 && charCode != 104 && charCode != 83 && charCode != 115 )
          {  
                alert("You must either leave blank or enter H or S!"); 
     			document.getElementById("f1").value = "";
          }else{
    	      if (charCode == 104  || charCode == 115 )
    		  {
    			var cUp = String.fromCharCode(charCode)
    			document.getElementById("f1").value = cUp.toUpperCase();
    		  }
    	  }
    
    }
    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?
  • Logician
    New Member
    • Feb 2007
    • 210

    #2
    It's much simpler to use a regular expression to remove unwanted characters from the string after each keystroke. Use the onkeyup event.

    Comment

    • Claus Mygind
      Contributor
      • Mar 2008
      • 571

      #3
      great do you have an example I can use. although I did get it to work like this:
      Code:
       onKeyPress=" return isValidCode(event, this);"
      
      function isValidCode(evt, obj)
      {
         evt = (evt) ? evt : event; 
         var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :  
             ((evt.which) ? evt.which : 0));   
      	if ( charCode == 8 || 
      		 charCode == 9 || 
      		(charCode > 47 && charCode < 58) || 
      		(charCode > 64 && charCode < 91) ||	
      		(charCode > 96 && charCode < 123) )
      	{
      		if (charCode > 96 && charCode < 123)
      		{
      			Here I would like to make it an uppercase value
      		}
      	}else{
      		alert("That is not a valid character ");
      		return false;
      	}
      }
      Perhaps your example of a regular expression can also convert the keystroke upper case if allowed?

      Comment

      • Claus Mygind
        Contributor
        • Mar 2008
        • 571

        #4
        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)

        Code:
        var charCode = event.keyCode;
        
        event.keyCode = charCode - 32
        And as I am working exclusively with fireFox that will not work for me.

        will look at the keyup option

        Comment

        Working...