Home > Web Front-end > JS Tutorial > body text

js限制文本框只能输入中文的方法_javascript技巧

WBOY
Release: 2016-05-16 15:45:52
Original
1159 people have browsed it

本文实例讲述了js限制文本框只能输入中文的方法。分享给大家供大家参考。具体如下:

让文本框只能输入中文的方法及代码,如果你不小心输入了英文,它会自动倒回去,清空你的输入,直至你输入了中文它可会继续,很有意思,也很实用,当注册一些资料的时候可以来提升输入的准确度。

运行效果如下图所示:

具体代码如下:

<html>
<head>
<title>让一个文本框只能输入汉字的方法</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<input onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))">试着在文本框里分别输入汉字和英文字符试试?
</body>
</html>

Copy after login

希望本文所述对大家的javascript程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!