Best books about Javasript
|
Input
an Chinese character in the following input box. If you didn't input
Chinese character, you will see the prompt.
Input Chinese Character
like: 你好.
How
to do it?
You need the following Javascript to detect Chinese character.
<script language="javascript">
<!--
//remove empty spaces
function trim(str)
{
return str.replace(/^\s*|\s*$/g,"");
}
function test()
{
var str=document.form1.name.value;
if (trim(str)=="")
{
alert("Input something, Chinese character only");
return;
}
var re1 = new RegExp("^[\u4E00-\uFA29]*$"); //Chinese character range
var re2 = new RegExp("^[\uE7C7-\uE7F3]*$"); //Chinese character range
str = str.replace(/(^\s*)|(\s*$)/g,'');
if (str == '')
{
alert("Oh, man, Please input Chinese character.");
return;
}
if (!(re1.test(str) && (! re2.test(str))))
{
alert("Oops, Please input Chinese character.");
return;
}
}
-->
</script> |
The next step you need create a Form to accept client input like
the following:
<form name="form1" method="post" action="">
<input type="text" name="name">
<input type="submit" name="Submit" value="Submit" onclick="test()">
</form> |
OK. Done. |