Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7902249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:26:45+00:00 2026-06-03T09:26:45+00:00

How can I prevent the user from inserting more than one line break in

  • 0

How can I prevent the user from inserting more than one line break in a row into a text field?
I prefer a solution using jQuery.

UPDATE 1:

I have inserted anadeo’s code into my code which looks like the following. Unfortunately it doesn’t work – it still allows more than one line break in a row in the textarea #posttext. Anyone know how to fix this? Thanks 🙂

//Post something
$('#postDiv').on('keydown', '#posttext', function(e) {
if (e.which==13 && !e.shiftKey && $('#posttext').val()!=' Post something...') {

    var last = false;

    var code = (e.keyCode ? e.keyCode : e.which);
    if(code == 13) {
        if (last) { e.preventDefault(); }
        last=true;
    }else{
        last=false;
    }

    //If not currently loading and enough text typed in, submit
    if($('#imageFeedback').html()!=' | Loading...' && $('#posttext').val().length>15){

        //Say that it is loading
        $('#imageFeedback').css('color','black').css('font-weight','normal').html(' | Loading...');

        //Call function to post
        post($(this));
        return false;
    }
}
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T09:26:47+00:00Added an answer on June 3, 2026 at 9:26 am
    $(textarea).on('keydown', function(e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        if(code == 13 && this.value.match(/\n/g)) e.preventDefault();
    });​
    

    FIDDLE

    ...more than one line break in a row
    

    Are you thinking of avoiding more than one line break in a row (one after another), like so:

    var last = false;
    
    $(textarea).on('keydown', function(e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        if(code == 13) {
            if (last) { e.preventDefault(); }
            last=true;
        }else{
            last=false;
        }
    });​
    

    FIDDLE

    EDIT:

    Firstly, on() is only supported in jQuery 1.7+, so make sure you are using a newer version of the library.

    You are also using the delegated version of on(), and that should’nt be a problem, but it assumes that your textarea is inserted into the DOM at a later time, and not present from the start, ie. it’s dynamically inserted with JS etc.

    The rest seems ok, but the last variable will have to be placed outside the keydown function to work properly, and the var code should be at the top, doing the check for the enter key only once, as the second check is not really necessary.
    The code variable is really optional, as jQuery is supposed to normalize e.which, but doing a check to see if keycode or which is supported seems like a good idea to ensure cross browser compatibility.

    The check for the #posttext value will only work once, as soon as a linebreak is entered the value is no longer Post something..., and the “limit linebreaks” function will be enabled?

    All in all I think this should work, try it and see ?

    var last = false;
    
    $('#postDiv').on('keydown', '#posttext', function(e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        if (code==13 && !e.shiftKey && $('#posttext').val()!=' Post something...') {
            if (last) { e.preventDefault(); }
            last=true;
        }else{
            last=false;
        }
    });​
    

    FIDDLE

    Also, It would probably be a good idea to use a less common variable name instead of last, something like MyLastTyped, PostTextLast etc. that is less likely to be confused with a function or variable with the name last in some other place or plugin etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I prevent a user from tabbing into a field (comboxbox)?
Is there a way that I can prevent a user from logging into a
In wpf how can i prevent user from moving the windows by dragging the
Is there any way I can prevent the user from hitting the return key
How can I prevent a user from accessing my app at example.appspot.com and force
How can I prevent a user from accessing a page when they are not
How can I prevent the user from being able to resize an image in
How can i prevent a user from running multiple instances of Air application? somebody
In Rails 2, how can I prevent a user from just changing the id
How can I prevent a user from resizing GridViewColumns withing a ListView control?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.