生活随笔
收集整理的這篇文章主要介紹了
限制用户不允许输入中文字符
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
應用JavaScript提供的excape()方法,該方法返回一個包含了charstring內容的字符串值,通過判斷轉換后的字符串是否包含“%u”,即可知道字符串中是否包含中文字符‘
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>限制用戶不允許輸入中文</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--><script language="javascript"> function checkCN(str){if (escape(str).indexOf("%u")<0){//indexOf()方法查找字符串是否包含"\u" ,如果不包含返回-1return true;} else {return false;}}function check(){var userName = document.getElementById("username").value;if(userName==""){alert("請輸入用戶名!");document.getElementById("username").focus();return;}if(!checkCN(userName)){alert("用戶名不允許包含中文字符!");document.getElementById("username").focus();return;}document.getElementById("myform").submit();}</script><style type="text/css">table{font-size: 13px;font-family: 楷體;color:navy;}input{font-size: 13px;font-family: 楷體;color:navy;}font{font-size: 12px;font-family: 楷體;color:orangered;}.style1{width: 500px;height: 160px;}</style></head><body><fieldset class="style1"><legend>用戶注冊</legend><form action="" id="myform"><table align="center"><tr><td>用戶名:</td><td><input type="text" id="username"> </td></tr> <tr><td>密碼:</td><td><input type="password" id="pwd"/></td></tr> <tr><td>確認密碼:</td><td><input type="password" id="pwd1"/></td></tr> <tr><td>性別:</td><td><input type="radio" name="sex" id="man" value="m" checked="checked" />男<input type="radio" name="sex" id="woman" value="f" />女</td></tr> <tr><td>年齡:</td><td><input type="text" id="age"/></td></tr> <tr><td colspan="2" align="center"><input type="button" value="注 冊" "check()"></td></tr></table></form></fieldset></body>
</html>
總結
以上是生活随笔為你收集整理的限制用户不允许输入中文字符的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。