1

I have html which is something like, not exactly like this, but just to give you an idea.

<input type="text" disabled="disabled" name="ip" />
<input type="text" name="name" />
<input type="text" name="email" />
<input type="submit" value="send" />

How to restrict special characters from using in only name field? I want only alphanumeric in name field.

Can you please help me do this?

Thanks in advance

NewbieDeveloper
  • 126
  • 2
  • 2
  • 10
  • 1
    There are various ways of doing this and I'm quite sure it's a few seconds googling. Additionnally, you may want to disallow numeric characters in that field but allow some special characters like the dot or the dash that you're more likely to find in a name than anything numeric... – Laurent S. Aug 22 '14 at 13:59

2 Answers2

4

Try this:

var alphanumers = /^[a-zA-Z0-9]+$/;
if(!alphanumers.test($("#firstname").val())){
    alert("Nickname can have only alphabets and numbers.");
}

DEMO

or

$(document).ready(function () {
    var charReg = /^\s*[a-zA-Z0-9,\s]+\s*$/;
    $('.keyup-char').keyup(function () {
        $('span.error-keyup-1').hide();
        var inputVal = $(this).val();

        if (!charReg.test(inputVal)) {
            $(this).parent().find(".warning").show();
        } else {
            $(this).parent().find(".warning").hide();
        }

    });
});

DEMO2

Suganth G
  • 5,136
  • 3
  • 25
  • 44
1

You can search on Google about "input mask" like this very good plugin : https://github.com/RobinHerbots/jquery.inputmask

Or this one too : http://digitalbush.com/projects/masked-input-plugin/

Arthur
  • 4,870
  • 3
  • 32
  • 57