如何用javascript检查一个字符串中是否有汉字以外的字符

来源:百度知道 编辑:UC知道 时间:2024/05/24 01:01:52
有现成的代码,但是会显示字符串中的HTML代码,如何才能除去这些代码,应该如何修改?

<SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT>
Function gotTopic(str,strlen)
if str="" then
gotTopic=""
exit function
end if
dim l,t,c, i
str=replace(replace(replace(replace(str," "," "),""",chr(34)),">",">"),"<","<")
l=len(str)
t=0
for i=1 to l
c=Abs(Asc(Mid(str,i,1)))
if c>255 then
t=t+2
else
t=t+1
end if
if t>=strlen then
gotTopic=left(str,i) & "..."
exit for
else
gotTopic=str
end if
next
gotTopic=replace(replace(replace(replace(gotTopic," "," "),chr(34),"""),">","

给你一个简单的,用JavaScript+正则表达式:

<html>
<head><title></title>
<script type="text/JavaScript">

function checkChinese()
{
var str=document.getElementById("txtTest").value;
var reg = /^[\u4e00-\u9fa5]+$/i;
if (reg.test(str)){
alert("文本框中都是中文");

}
else{
alert("文本框中有除中文外的字符");
}
}
</script>
</head>
<body >
<center>
请输入字符:<input name="txtTest" /><br>

<input type="button" name="btmTest" value="检测中文" onclick="checkChinese();" />
</center>
</body>
</html>