javascript限制用户只能输汉字中文的方法
作者:shichen2014 时间:2023-07-02 05:30:29
本文实例讲述了javascript限制用户只能输汉字中文的方法。分享给大家供大家参考。具体实现方法如下:
要验证函数时我们必须明白,如果是汉字,则字符串长度加2,如果用正则我们直接使用\\u4E00-\\u9FA5 即可解决。
一、Unicode检验汉字
function chkstrlen(str)
{
var strlen = 0;
for(var i = 0;i < str.length; i++)
{
if(str.charCodeAt(i) > 255) //如果是汉字,则字符串长度加2
strlen += 2;
else
strlen++;
}
return strlen;
}
二、利用正则只能输入汉字的
<input onkeyup="value=value.replace(/[^u4E00-u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^u4E00-u9FA5]/g,''))">
希望本文所述对大家的javascript程序设计有所帮助。
标签:javascript,限制,汉字,中文
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
无法在发生错误时创建会话,请检查 PHP 或网站服务器日志,并正确配置 PHP 安装最快的解决办法
2023-09-03 19:49:50
Pyhon Flask框架:第一个Flask程序
2023-01-09 16:06:34
![](https://img.aspxhome.com/file/2023/3/81833_0s.png)
Python3 修改默认环境的方法
2023-04-01 16:23:36
编写安全的SQL Server扩展存储过程
2008-11-25 11:16:00
Python OpenCV实现图片上输出中文
2021-07-01 15:00:11
![](https://img.aspxhome.com/file/2023/3/115003_0s.jpg)
MSSQL优化之探索MSSQL执行计划(转)
2011-11-03 17:16:21
Python 时间操作time详情
2023-09-13 13:23:09
![](https://img.aspxhome.com/file/2023/6/98326_0s.png)
python3 深浅copy对比详解
2023-11-08 11:12:51
Oracle备库宕机启动的完美解决方案
2023-07-20 05:28:34
![](https://img.aspxhome.com/file/2023/9/64079_0s.png)
Pandas之缺失数据的实现
2022-05-04 19:00:05
![](https://img.aspxhome.com/file/2023/9/115099_0s.png)
SQL触发器在保持数据库完整性中的实际应用
2009-10-23 14:34:00
python爬虫lxml库解析xpath网页过程示例
2021-10-06 20:01:54
![](https://img.aspxhome.com/file/2023/4/134534_0s.png)
python GUI库图形界面开发之PyQt5单选按钮控件QRadioButton详细使用方法与实例
2022-01-26 01:33:47
![](https://img.aspxhome.com/file/2023/7/115507_0s.png)
python引入requests报错could not be resolved解决方案
2022-08-28 08:24:45
![](https://img.aspxhome.com/file/2023/0/79710_0s.png)
pycharm创建scrapy项目教程及遇到的坑解析
2022-05-02 12:55:38
![](https://img.aspxhome.com/file/2023/2/66782_0s.png)
Task List 管理任务JavaScript源码
2010-01-22 15:43:00
使用Tensorflow-GPU禁用GPU设置(CPU与GPU速度对比)
2022-06-10 19:10:11
![](https://img.aspxhome.com/file/2023/1/85081_0s.jpg)
Golang中Bit数组的实现方式
2024-06-07 16:06:34
一篇文章带你了解python正则表达式的正确用法
2023-01-05 19:20:30
![](https://img.aspxhome.com/file/2023/6/133806_0s.png)
Python计算信息熵实例
2022-08-17 16:13:39
![](https://img.aspxhome.com/file/2023/0/103300_0s.jpg)