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
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
Perhaps you want something like this:
var input = "f%o@o";
var output = input.replace(/\W/g, ''); // "foo"
This will remove any non -word character (a word character is a letter, number, or underscore) from the input
string.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.