javascript - JQuery function to allow only alphabets in textbox not working -
i use following jquery function restrict users writing numeric values in textbox. code works fine problem restrict users using other characters full stop(.), comma(,), $, @ , other signs..it not allow users use options of copy , past. want restrict users writing numeric values or numbers user should allowed use other characters.
$(function() { $('.txtonly').keydown(function(e) { if (e.shiftkey || e.ctrlkey || e.altkey) { e.preventdefault(); } else { var key = e.keycode; if (!((key == 8) || (key == 32) || (key == 46) || (key >= 35 && key <= 40) || (key >= 65 && key <= 90))) { e.preventdefault(); } } }); });
hope helps:
<!doctype html> <html lang="en"> <head> <script src="jquery-1.12.2.js"></script> </head> <body> <input class="txtonly" id="txtonly" name="txtonly" type="text" /> <script> $( document ).ready(function() { $( ".txtonly" ).keypress(function(e) { var key = e.keycode; if (key >= 48 && key <= 57) { e.preventdefault(); } }); }); </script> </body> </html>
Comments
Post a Comment