SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    England
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Disallowing carriage returns with Javascript

    Hi

    I have a text area and want to disallow people from using a carriage return, so when a user hits the carriage return button nothing happens (I will set up an alert stating that they can't use it)

    Any ideas?

    Thanks

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,457
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)

    no newlines

    If you don't want them to enter newlines, why not simply use a (long) text input instead of a textarea?

  3. #3
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is a bad idea- but it's your site.

    Code:
    var who=document.getElementsByTagName('textarea')[0];
    who.onkeydown= function(e){
    	var e=window.event || e;
    	if(e.keyCode==13) return pauseEvent(e);
    }
    
    function pauseEvent(e){
    	if(window.event){
    		e= (e && e.srcElement)? e: window.event;
    		if(!e) return false;
    		e.cancelBubble= true;
    		if(e.returnValue) return (e.returnValue= false);
    		return false;
    	}
    	if(e){
    		if(e.stopPropagation) e.stopPropagation();
    		if(e.cancelable && e.preventDefault) e.preventDefault();
    	}
    	return false;
    }

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,457
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)

    interferring with functionity

    Quote Originally Posted by mrhoo
    a bad idea
    That's an under-statement. Users come to expect certain behaviour from certain page elements. Reset buttons reset, Submit buttons submit, Text inputs accept a word or single line of text, and textareas accept multiple lines of text. Even with a alert, stubborn users might "copy paste" the cr, but most will leave and not return. I have the feeling you taking the wrong approach to solving a problem here.
    Perhaps if you explain why you don't want the textareas to behave normally a better solution could be found.


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
  •