Using Jquery – Remove Space and special characters while typing

Whenever you need to remove “space” as well as “special characters” between characters while typing, you can do it easily using jQuery as described below :-

Checkout this code in action :- http://jsfiddle.net/hn9wk/18/

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>
<input type="text" name="text_box_id" id="text_box_id" />
<script>
    $("#text_box_id").change(function() {
        var name = $(this).val();
        var dname_without_space = $("#text_box_id").val().replace(/ /g, "");
        var name_without_special_char = dname_without_space.replace(/[^a-zA-Z 0-9]+/g, "");
        $(this).val(name_without_special_char);
    });
</script>
</html>
Advertisements

4 thoughts on “Using Jquery – Remove Space and special characters while typing

I like to hear from you about this !!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s