javascript - How to Restrict to take input in JQuery from a input field Consisting on Alphabets without Space -
i want take input text field , value should consisting on alphabets , there should not space in between? kindly let me know how achieve this? code follows;
$("#xxxx").bind("keyup change", function () { $(this).val($(this).val().replace(/ /g,"")); });
get know regexes. fun, , can test them here: https://regex101.com/
use regex in replace finds non-alpha characters:
// no-conflict-safe document ready jquery(function($) { // bind using on *class* instead of id. $(".numbers-only").on("keyup blur", function() { // use regex finds non-alpha characters $(this).val($(this).val().replace(/[^a-z]/ig, '')); }); });
working fiddle: https://jsfiddle.net/cale_b/602wuwmx/
notes improving code:
1. use on instead of bind. (per bind
documentation, bind superseded .on() method attaching event handlers document since jquery 1.7, use discouraged.)
2. bind class rather id. in way, can make multiple inputs behave way.
Comments
Post a Comment