How to prevent users from entering invalid characters inside an input field

后端 未结 4 1912
孤街浪徒
孤街浪徒 2020-12-21 17:33

Given a text input field. How can I prevent users from entering spaces, and other other than letters numbers or dashes (-).

Alphanumerics only - \"The alphanumeric

4条回答
  •  时光说笑
    2020-12-21 18:13

    You can do this using the jQuery keyup(..) method. You will want to check that the event.keyCode is something valid. If it is not valid, you can prevent the event with preventDefault().

    Remember to validate the data sent to the server because anything you do in javascript can be subverted.

    Here is a library to do it for you: http://www.itgroup.com.ph/alphanumeric/

提交回复
热议问题