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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:39:01+00:00 2026-06-13T03:39:01+00:00

I have a simple implementation of some real time validation for an input. The

  • 0

I have a simple implementation of some “real time” validation for an input. The allowed characters are lower and uppercase alpha, 0-9, hyphens, period, apostrophe, and spaces. The field is limited to 15 characters. I’m using jQuery here simply for ease of use in my example, it may or may not be used for my project.

Currently when trying to place/move the cursor within the input using the arrow keys I’m always taken to the end of the string. The same happens when trying to select all text using keys (CTRL-A). Is there a better way of accomplishing what I’m looking for that avoids this? It seems like poor user experience to me.

$( '#sample' ).on( 'keyup', function( event ) {
    var str = $( this ).val();
    str = str.replace(/[^A-Za-z-0-9.'\s]/g, "").substring(0,15);
    $( this ).val( str );
});​

I also have a jsFiddle set up here

  • 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-13T03:39:03+00:00Added an answer on June 13, 2026 at 3:39 am

    Most of the issues can be preventent just by updating the input value only if it actually changes.

    $( '#sample' ).on( 'keyup', function( event ) {
    
       var str = $( this ).val();
       str = str.replace(/[^A-Za-z-0-9.'\s]/g, "").substring(0,15);
    
       if($( this ).val() != str) {
           $( this ).val( str );
       }
    });​
    

    To solve removing characters from the middle of the text you need to remember and reset caret position. For that you can use following functions:

    $.fn.getCaretPosition = function() {
        var el = $(this).get(0);
        var pos = 0;
        if ('selectionStart' in el) {
            pos = el.selectionStart - 1;
        } else if ('selection' in document) {
            el.focus();
            var Sel = document.selection.createRange();
            var SelLength = document.selection.createRange().text.length;
            Sel.moveStart('character', -el.value.length);
            pos = Sel.text.length - SelLength;
        }
        return pos;
    }
    
    $.fn.setCaretPosition = function(pos) {
        if ($(this).get(0).setSelectionRange) {
            $(this).get(0).setSelectionRange(pos, pos);
        } else if ($(this).get(0).createTextRange) {
            var range = $(this).get(0).createTextRange();
            range.collapse(true);
            range.moveEnd('character', pos);
            range.moveStart('character', pos);
            range.select();
        }
    }​
    

    And extend your keyup handler:

    $('#sample').on('keyup', function(event) {
    
        var str = $(this).val();
        str = str.replace(/[^A-Za-z-0-9.'\s]/g, "").substring(0, 15);
    
        if ($(this).val() != str) {
            var pos = $(this).getCaretPosition();
            $(this).val(str);
            $(this).setCaretPosition(pos);
        }
    });
    

    See updated FIDDLE.

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

Sidebar

Related Questions

We have in place a simple implementation of GA and have had for some
I have a simple html multiline tooltip implementation: this.tooltip = function(tag) { xOffset =
I have tab controller created through storyboards with a custom implementation (simple buttons and
I have simple php validation form that is halfway working. If you leave the
I have a problem with some simple code. The photo's, either picked or taken
I have made a simple implementation of INotifyDataErrorInfo in a WPF 4.5 project. This
I seem to have a problem with my very simple implementation of a file
I have a very simple implementation of the DefaultModelBinder, I need it to fire
I am using John resig's implementation class mentioned here: http://ejohn.org/blog/simple-javascript-inheritance/ Now I have a
I have a fairly simple set of functionality for which I have multiple implementations,

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.