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 8869487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:29:29+00:00 2026-06-14T17:29:29+00:00

I use a javascript code to clean the username on sign up. When a

  • 0

I use a javascript code to clean the username on sign up. When a character is not allowed, it’s replaced by a dash.

My problem is : when the users wants to add characters in the middle of the text, the cursor is automatically placed at the end of the input.

You can test it here : http://jsfiddle.net/tZv5X/

HTML :

Username : <input type="text" id="username" />​

JS :

// Clean username
function clean_username(s) 
{
    var temp = s.replace(/[àâä@]/gi,"a");
    temp = temp.replace(/[éèêë]/gi,"e");
    temp = temp.replace(/[îï]/gi,"i");
    temp = temp.replace(/[ôö]/gi,"o");
    temp = temp.replace(/[ùûü]/gi,"u");
    temp = temp.replace(/[ç]/gi,"c");
    temp = temp.replace(/[. _,;?!&+'"()]/gi,"-");
    return temp;
}

var current_value;
$("#username").keyup(function(e)
{
    if($(this).val() != current_value)
    {
        $(this).val(clean_username($(this).val()));
        current_value = $(this).val();
    }
});​

Any idea ?

Thanks

  • 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-14T17:29:30+00:00Added an answer on June 14, 2026 at 5:29 pm

    You may use jCaret plugin (also mentioned by @puppybeard). Check it out:

    $("#username").bind({
        keydown: function() {
            var $this = $(this);
            $this.data("pos", $this.caret().start);
        },
        keyup: function() {
            var $this = $(this),
                pos = $this.data("pos"),
                value = clean_username(this.value);
            if (value !== this.value) {
                this.value = value;
                $this.caret(pos + 1, pos + 1);
            }
        }
    });​
    

    DEMO: http://jsfiddle.net/tZv5X/3/

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

Sidebar

Related Questions

I'm coding up a page where I only want to use raw JavaScript code
Here is the javascript code I use to create the array and send it
In my code, I use JavaScript for UI and PHP for back end. I
I use Aptana Studio to code JavaScript. When I write string with </ ,
the javascript:alert code that is being dynamically created ONLY works if I use the
I have the following in my HTML5 code which makes use of javascript: var
I need to use a code in JavaScript to determine a user, I decided
Looking to pass variables from c# to javascript to use some jquery code. Passing
sometimes i use debug code to alert something in javascript (for example, matching something
I am trying to use some rails code withing a javascript and need to

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.