如何防止用户在输入字段中输入无效字符 [英] How to prevent users from entering invalid characters inside an input field

查看:181
本文介绍了如何防止用户在输入字段中输入无效字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定文本输入字段。如何阻止用户输入空格以及除字母数字或短划线( - )以外的其他内容。

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

仅限字母数字 - 字母数字字符集由数字0到0组成在perl编程语言中,下划线字符(_)也被认为是字母数字字符集的成员

Alphanumerics only - "The alphanumeric character set consists of the numbers 0 to 9 and letters A to Z. In the perl programming language, the underscore character ( _ ) is also considered to be a member of the alphanumeric set of characters"

这是对于用户可以选择自定义URL的字段。我想阻止用户输入无效字符。

This is for a field where users can pick a custom url. I would like to prevent users from entering invalid characters.

想法?谢谢

推荐答案

你可以使用jQuery keyup(..) 方法。您需要检查 event.keyCode 是否有效。如果它无效,您可以使用 preventDefault()

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().

请记住验证发送到服务器的数据,因为您在javascript中执行的任何操作都可能被破坏。

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

这是一个为您完成的图书馆: http:// www。 itgroup.com.ph/alphanumeric/

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

这篇关于如何防止用户在输入字段中输入无效字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆