jquery - Allow only space, underscore , alphabets and numbers in text box -


i have code allows characters in text box. allows alphabets , numbers first character. now, how allow space , underscore along alphabets , numbers? want code in jquery. found similar solution on here doesn't contain jquery code. http://jsfiddle.net/fwcfq/39/

    $('#value').bind('keypress', function(e) {         if($('#value').val().length == 0){             if (e.which == 32){//space bar                 e.preventdefault();             }             var valid = (e.which >= 48 && e.which <= 57) || (e.which >= 65 && e.which     <= 90) || (e.which >= 97 && e.which <= 122);             if (!valid) {                e.preventdefault();             }         }     });  

this code want

$('#value').bind('keypress', function (e) {     if ($('#value').val().length == 0) {         if (e.which == 32) { //space bar             e.preventdefault();         }         var valid = (e.which >= 48 && e.which <= 57) || (e.which >= 65 && e.which <= 90) || (e.which >= 97 && e.which <= 122);         if (!valid) {             e.preventdefault();         }     } else {         var valid = (e.which >= 48 && e.which <= 57) || (e.which >= 65 && e.which <= 90) || (e.which >= 97 && e.which <= 122 || e.which == 32 || e.which == 95 || e.which == 8);         if (!valid) {             e.preventdefault();         }     } }); 

fiddle

this approach assumes know keys want accept however. instance enter key rejected unless explicitly allow it.


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -