Skip to content

Instantly share code, notes, and snippets.

@z007
Last active August 29, 2015 14:08
Show Gist options
  • Save z007/d64441a266af8ad7dc45 to your computer and use it in GitHub Desktop.
Save z007/d64441a266af8ad7dc45 to your computer and use it in GitHub Desktop.
输入时清空输入框如果有汉字提示有几个汉字 不输入返回默认值
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>输入时清空输入框如果有汉字提示有几个汉字 不输入返回默认值 </title>
<script>
function clearText(){
myForm.myName.value='';
}
function returnText(){
if(myForm.myName.value==''){
document.myForm.myName.innerText='请在此输入姓名:';
}else {cal()}
}
function cal(){
var str=myForm.myName.value;
re=/[\u4E00-\u9FA5]/g;//测试中文字符的正则
if(re.test(str))//使用正则表达式判断是否存在中文
{ var num=str.match(re).length;
alert('汉字'+num+'个数')
}
else{alert("请输入汉字");}
}
</script>
</head>
<body>
<form name='myForm' action="" method="post">
姓名<input name="myName" type="text" value="请在此输入姓名:" onfocus="clearText()"onBlur="returnText()" />
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment