SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Thread: space problem

  1. #1
    SitePoint Evangelist
    Join Date
    Jan 2005
    Location
    bahrain
    Posts
    553
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up space problem

    hi
    i want to know how can i only restrict a user that when he or she presses space bar to put space it will not enter in text fileld means every thing can enter except space.
    its urgent plzzz.....
    regards
    phphelp

  2. #2
    SitePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could you break that into shorter sentences? I'm having trouble following what is supposed to happen when.

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    1) Moz automatically sends the event object, which contains the key code of the key that was pressed, to the event handler function. So, you need to 'catch' the event object by adding a parameter to your function:
    Code:
    function cancel_Key(e)
    {
    	....
    	....
    	
    }
    IE does not automatically send the event object to the function, so you have to summon it manually:
    Code:
    function cancel_Key(e)
    {
    	if (!e) var e = window.event;  //for IE
    	
    }
    After that, you have the event object stored in the variable 'e'.

    2) There are also cross browser differences as to where the key code is stored in the event object 'e'. In IE, it is in the .keyCode property. In Moz, it can be the .which property. So, you need to do something like this:
    Code:
    function cancel_Key(e)
    {
    	if (!e) var e = window.event;  //for IE
    	
    	var code;
    	if(e.keyCode) code = e.keyCode; //for IE
    	if(e.which) code = e.which;  //for other browsers
    }
    After that, the variable 'code' will have the key code.

    3) Then, all you have to do is test for the keycode you are interested in canceling, and return false to cancel the keypress:
    Code:
    function cancel_Key(e)
    {
    	if (!e) var e = window.event;  //for IE
    	
    	var code;
    	if(e.keyCode) code = e.keyCode; //for IE
    	if(e.which) code = e.which;  //for other browsers
    
    	if (code == " ".charCodeAt(0)) return false;
    	else return true;
    }
    Some browsers allow the canceling of the onkeydown, onkeyup, and onkeypress events, and some don't, so that can be another problem. I believe onkeydown is not cancelable in FF1.0, where onkeypress is cancelable in IE6 and FF1.0.

    4) Now, how do you use that function? If you want to cancel the key on the whole page:
    Code:
    window.onload=function()
    {
    	document.onkeypress = cancel_Key;
    };
    </script>
    </head>
    If you want to cancel the key on only a form:
    Code:
    window.onload=function()
    {
    	document.my_form.onkeypress = cancel_Key;
    };

  4. #4
    SitePoint Evangelist
    Join Date
    Jan 2005
    Location
    bahrain
    Posts
    553
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i want to know that if u user wants to put space between charcters he cannot enter space. i thik i have cleared now.
    its urgent plzzz.....
    regards
    phphelp

  5. #5
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should check for spaces with whatever server side language you are using in case JavaScript is not enabled in the user's browser for some reason.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  6. #6
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Kravvitz has a point, but try this, just for giggles:

    Code:
       <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
       <html>
       <head>
       <title>Untitled</title>
       <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
       </head>
       <body>
       <form action="">
       <p>
       <input type="text" onkeyup="this.value=this.value.replace(/\s/g,'');" />
       </p>
       </form>
       </body>
       </html>

  7. #7
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <input type="text" onkeyup="this.value=this.value.replace(/\s/g,'');" />
    You probably want the g (global) modifier.

    While Netscape 4+ does support String.charCodeAt(), support for it wasn't added to IE until version 5.5, so you may not want to use 7stud's elegant solution.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  8. #8
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kravvitz
    While Netscape 4+ does support String.charCodeAt(), support for it wasn't added to IE until version 5.5, so you may not want to use 7stud's elegant solution.
    If that is a problem, you can replace the " ".charCodeAt(0) with the actual code for a space, which is 32:
    Code:
    if (code == 32) return false;
    else return true;
    There is an argument for using a replace() instead: it creates a kind of eraser, which is apparent to the user. If you have to cancel a lot of keys, the user may think their keyboard is locked up or not working correctly. Choose whatever effect you like best.

  9. #9
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    phphelp

    Try this

    Code:
    <input type="text"
    	onkeypress="return (event.keyCode || event.which) != 32" />

  10. #10
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The only problem with just capturing keystrokes in real time is the same one the g (global) modifier in the regular expression within the replace() method addresses: What if the user cuts and pastes in a string with multiple spaces?

    Admittedly, the jumping cursor in the replace() technique is a bit distracting, but I like to think of it as emphatically telling the user, "No!" Sort of a, "No! Bad user! Bad!!" Kind of gratifying to get back at the needy little program-breakers, don't you think? Something of a virtual slap on the wrist, as it were.
    Last edited by JVLB; Mar 29, 2005 at 07:03.

  11. #11
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "onkeyup" approach doesn't help in 'paste' case, because text can be changed with mouse as well via context menu and drag-and-drop...

    And there is no such thing as "bad user".

  12. #12
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    All the more reason to take their bloody little mousies away, I say! Make them use a command line and be done with it. Barring that, backup code on the onchange event would at least clean up the mess after focus moves, as only IE seems to support an onpaste event. Onfocus might offer some utility here, as well, but might not be truly reliable.


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
  •