SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    May 2007
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Excluding non-printing characters

    Hello. I try to retireve a textarea value when user press a key on keyboard. I added a keyup event listener to textarea but I dont want javascript alert when non-printing character keys such as CTRL and TAB is pressed. How can I get alerted when just the keys that are used to edit textarea value e.g printing characters, numbers and backspace etc. is pressed?

    Thanks in advance.

    PHP Code:
    <html>
    <
    head>
    <
    title>Trial</title>
    <
    script type="text/javascript">

    function 
    keypresso () {
    alert(document.getElementById("main").value);
    }

    window.onload=function () {

    document.getElementById("main").addEventListener("keyup",keypresso,false);

    }
    </script>

    </head>
    <body >

    <textarea id="main"></textarea>

    </body>
    </html> 

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    When the event triggers the function, you check for the special keycodes (they're less than 32)
    See http://www.quirksmode.org/js/keys.html
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Addict
    Join Date
    May 2007
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    When the event triggers the function, you check for the special keycodes (they're less than 32)
    See http://www.quirksmode.org/js/keys.html
    Hello. Thank you for the answer. I think I am going to create almost 32 booleans in one if clause for excluding the unwanted characters. It doesnt seem to be efficient. Is creating booleans the only way for excluding non-printing characters?

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Code javascript:
    function keypresso () {
        if (keyCode < 32) {
            return;
        }
        // carry on processing keyCode
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    function keypresso (e){
    	e= window.event || e;
    	e= e.target || e.srcElement;
    	if(String.fromCharCode(e.keyCode)== '') {
    		// handle non printing character;
    	}
    	//handle printing character
    }
    window.onload= function (){
    	var who= document.getElementById("main");
    	if(who.addEventListener)who.addEventListener("keyup",keypresso,false);
    	else if(who.attachEvent) who.attachEvent("onkeyup",keypresso)
    }

  6. #6
    SitePoint Addict
    Join Date
    May 2007
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    Code javascript:
    function keypresso () {
        if (keyCode < 32) {
            return;
        }
        // carry on processing keyCode
    }

    Thank you again @pmw57. It is great shortcut.
    Quote Originally Posted by mrhoo View Post
    Code:
    function keypresso (e){
    	e= window.event || e;
    	e= e.target || e.srcElement;
    	if(String.fromCharCode(e.keyCode)== '') {
    		// handle non printing character;
    	}
    	//handle printing character
    }
    window.onload= function (){
    	var who= document.getElementById("main");
    	if(who.addEventListener)who.addEventListener("keyup",keypresso,false);
    	else if(who.attachEvent) who.attachEvent("onkeyup",keypresso)
    }
    Thank you @mrhoo. I changed your code a little bit but it didnt work on either FF or IE.
    Code JavaScript:
    function keypresso (e){
    	e= window.event || e;
    	e= e.target || e.srcElement;
     
    	if(String.fromCharCode(e.keyCode)==' ') {
    		alert("non-printing character");
    		return;
    	}
    	alert("printing character");
    }
    window.onload= function (){
    	var who= document.getElementById("main");
    	if(who.addEventListener) who.addEventListener("keyup",keypresso,false);
    	else if(who.attachEvent) who.attachEvent("onkeyup",keypresso)
    }


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •