JavaScript Regex javascript allow only numbers, alphabets and underscore regex javascript allow spaces,regex javascript allow special characters,regex

I'm using this code to prevent people from puting spaces between alphabets and numbers. I would lilke to modify the code so that only alphabets, letters and numbers can be typed.

This is what i'm doing:

 replace(/\sg, '') oin javascript

Can anyone help me as to how to costrucrt the regex expression so that only alphavets, nnumbers, underscore and no space can be keyed in the input box

Thanks

Answer:1

Here's how you could do it with jQuery:

$(function() {
    $( '#username' ).on( 'keydown', function( e ) {
        if( !$( this ).data( "value" ) )
             $( this ).data( "value", this.value );
    });
    $( '#username' ).on( 'keyup', function( e ) {
        if (!/^[_0-9a-z]*$/i.test(this.value))
            this.value = $( this ).data( "value" );
        else
            $( this ).data( "value", null );
    });
});

SQL Fiddle Demo

Answer:2

I'm trying to submit a form with AJAX, the form being content of jQuery UI Dialog. I use the same code as in jQuery's documentation but I modified the form so there is only one text input, so the ...

I'm trying to submit a form with AJAX, the form being content of jQuery UI Dialog. I use the same code as in jQuery's documentation but I modified the form so there is only one text input, so the ...

In ECMAScript 6, I have two way to define a dictionary: Traditional method (with dictionary = Object.create(null)): set property: dictionary.key = value or dictionary[key] = value get property: ...

In ECMAScript 6, I have two way to define a dictionary: Traditional method (with dictionary = Object.create(null)): set property: dictionary.key = value or dictionary[key] = value get property: ...

I like the off canvas feature that bootstrap 3 has: http://getbootstrap.com/examples/offcanvas/. However, for one of my projects i'd like it to overlap the content rather than move the content to the ...

I like the off canvas feature that bootstrap 3 has: http://getbootstrap.com/examples/offcanvas/. However, for one of my projects i'd like it to overlap the content rather than move the content to the ...

I would like to be able to detect if an object I'm looking at is an instance of _.template, the same way I can check for Backbone Models/Collections/Views. for example: var newView = new Backbone....

I would like to be able to detect if an object I'm looking at is an instance of _.template, the same way I can check for Backbone Models/Collections/Views. for example: var newView = new Backbone....