案例:验证用户输入的是不是中文名字
[\u4e00-\u9fa5]
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>title</title> </head> <body> 请输入您的名字:<input type="text" value="" id="userName" />*<br /> <script> //是中文名字,则绿色,否则红色 document.getElementById("userName").onblur = function () { var reg = /^[\u4e00-\u9fa5]{2,6}$/; if (reg.test(this.value)) { this.style.backgroundColor = "green"; } else { this.style.backgroundColor = "pink"; } }; </script> </body> </html>
原文地址:https://www.cnblogs.com/jane-panyiyun/p/12192392.html
时间: 2024-11-06 20:54:54