验证中文名字—正则表达式
效果
正确演示
错误演示
运用知识点
正则表达式
ps http://www.bejson.com/convert/unicode_chinese/
unicode编码(了解)
打开浏览器 F12 控制台
输入
escape(“淦”)
unescape("%u6DE6")
在这里插入图片描述
原理
如果输入的是中文名字 那么背景颜色为绿色 否则为红色
文本框注册失去焦点事件1. 获取文本框的值 2. 判断文本框的值是否属于邮箱格式并添加相应的样式
<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>验证中文名字</title></head><body>请您输入姓名:<input type="text" value="" id="name" /> *<br /><script>//如果输入的是中文名字 那么背景颜色为绿色 否则为红色// /[\u4e00-\u9fa5]/ unicode编码function my$(a){return document.getElementById(a);}my$("name").addEventListener("blur",function(){if(/^[\u4e00-\u9fa5]{2,6}$/.test(this.value)){this.style.backgroundColor="green";}else{this.style.backgroundColor="red";}});</script></body>
</html>