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

  • Home
  • SEARCH
  • 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 8892239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:56:58+00:00 2026-06-14T22:56:58+00:00

Possible Duplicate: See if Input Has Spaces with jQuery I have this code for

  • 0

Possible Duplicate:
See if Input Has Spaces with jQuery

I have this code for no spaces. Well atleast I thought it was for no spaces in username in registration…Now looking at it it is for every single key up! Can someone help me to make it spaces only?

(function($){
    $(function(){
        $('#username_reg').keyup(function(){
            str = $(this).val(),
            str = str.replace(/\s/g,''),
            $(this).val(str),
            alert('No space are allowed in usernames');
        });
    });
})(jQuery);

I tried this code as well just now, did not work :/

$("input#username_reg").on("keydown", function (e) {
    return e.which !== 32;
 });​
  • 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-14T22:56:59+00:00Added an answer on June 14, 2026 at 10:56 pm

    I believe the following code will do what you want:

    jQuery(function($){
        $('#username_reg').keyup(function(e){
            if (e.which === 32) {
                alert('No space are allowed in usernames');
                var str = $(this).val();
                str = str.replace(/\s/g,'');
                $(this).val(str);            
            }
        }).blur(function() {
            var str = $(this).val();
            str = str.replace(/\s/g,'');
            $(this).val(str);            
        });
    });
    

    That is, on keyup, if the key was a space display the alert with the message and remove the space. But also, on blur remove any spaces that might be in the field – because the user may copy/paste or drag’n’drop without using the keyboard.

    Demo: http://jsfiddle.net/wP8mP/

    EDIT: If it were my code I would probably remove the alert altogether – I’d just prevent spaces being typing by calling event.preventDefault() on key down, and still remove spaces on blur in case of a copy/paste.

    $('#username_reg').keydown(function(e){
        if (e.which === 32) {
            e.preventDefault();      
        }
    }).blur(function() {
        // for variety's sake here's another way to remove the spaces:
        $(this).val(function(i,oldVal){ return oldVal.replace(/\s/g,''); });         
    });
    

    Demo: http://jsfiddle.net/wP8mP/1/

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

Sidebar

Related Questions

Possible Duplicate: jQuery attribute selector for multiple values I have this $('input:radio[name=foo]').change(function() { blah();
Possible Duplicate: Merging jQuery objects See Demo a = $('div:eq(0)'); b = $('div:eq(1)'); c
Possible Duplicate: Checkbox Stays Checked on Page Refresh Please see the image carefully. This
Possible Duplicate: What is the cost of '$(this)'? I constantly see in some developers
Possible Duplicate: Why Java does not see that Integers are equal? I have 2
Possible Duplicate: problem when cloning jQuery UI datepicker I have a problem with the
Possible Duplicate: Javascript create variable from its name The code below checks to see
Possible Duplicate: jQuery Browser Compatability (IE) I would initially post some code snippets (and
Possible Duplicate: C#: what is the difference between i++ and ++i? I see this
Possible Duplicate: the code “ : ” in php I often see a lot

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.