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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:22:30+00:00 2026-05-16T18:22:30+00:00

$(function(){ $(‘input[name=username]’).keydown(function(key){ if (jQuery.inArray(key.keyCode, [8,37,39,48,49,50,51,52,53,54,55,56,57,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90]) == -1 || key.shiftKey ){ return false; } });

  • 0
$(function(){
    $('input[name="username"]').keydown(function(key){
        if (jQuery.inArray(key.keyCode, 
            [8,37,39,48,49,50,51,52,53,54,55,56,57,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90])
             == -1 || key.shiftKey ){
                return false;
        }
    });
});

What this does is Ajax validation on the text characters, it only lets me enter letters from a-z and 0-9, and not any other characters, but i want it to allow me the _ (underscore) character as well.

  • 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-05-16T18:22:30+00:00Added an answer on May 16, 2026 at 6:22 pm
    $('input[name="username"]').keypress(function(event) {
      var k = event.which;
      /* 0:      non-input keys
         8:      backspace
         48-57:  0-9
         97-122: a-z
         95:     _ */
      if (k != 0 && k != 8 && k != 95 &&
          (k < 48 || k > 57) && (k < 97 || k > 122))
        return false;
    });
    
    $('input[name="username"]').keyup(function() {
      var inp = $(this).val();
    
      if (inp.match(/[^a-z0-9_]/g))
        $(this).val(inp.replace(/[^a-z0-9_]/g, ''));
    });
    

    The keypress event handler tries to catch invalid characters before they are input into the field, to prevent a distracting ‘jumping’ effect when invalid characters are shown and on keyup are removed again. Note that event.which contains the character code, normalized by jQuery.

    The keyup handler is necessary to catch characters that are input using Ctrl-V (note that it unfortunately doesn’t catch input using the menu option Edit > Paste). The extra test is necessary to stop the input field from getting back to its most left position after every keyup (caused by changing its value). Now it only resets its position when there is something to be removed.

    You can test it on JSBin.

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

Sidebar

Related Questions

function safe(){ if($(this).is(':checked')){ $(select[name='sort']).attr(disabled, disabled); $(input[name='group']).attr(disabled, disabled) } else { $(select[name='sort']).attr(disabled, false); $(input[name='group']).attr(disabled, false)
function Apple(){ this.name=apple; } function Orange(){ this.name=orange; this.apple = new Apple(); this.apple.onCalled=function(){ alert(this.name); }
function Person() {} Person.prototype.population = 0; Person.prototype.constructor = function(name) { this.name = name; console.log(Name:
function getDbValue() { alert($('[data-bind]').length); alert($('[data-bind][0].data-bind')); alert($('[data-bind][0].value')); jQuery.each($('[data-bind]'), function(databind,key) { alert(key); alert(databind); alert(databind[key].data-bind); }) }
function myClass() { this.nums = [1,2,3]; this.divisor = 2; } myClass.prototype.divideNumsByDivisor = function(){ return
function MySingletonClass(arg) { this.arr = []; if ( arguments.callee._singletonInstance ) return arguments.callee._singletonInstance; arguments.callee._singletonInstance =
function get_denomination(){ return Fabrice Petard; } function auto_flatterie(flatterie){ // return this.get_denomination() + est +
function anchor($text) { return preg_replace('#\&gt;\&gt;([0-9]+)#','<span class=anchor><a href=#$1>>>$1</a></span>', $text); } This piece of code is
function tournamentViewModel(){ var self= this; self.name = ko.observable(); self.districts = ko.observableArray([new district('Provo',1),new district('Salt Lake
function json (url){ $.getJSON(url, function(data) { return data; }) } this function don't see

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.