Entry
Can I prevent a user from entering (either by typing or pasting) foreign-language (such as Chinese) characters in a form field?
Feb 13th, 2006 23:32
mallikarjun swamy, Henrik Jönsson, Matthew Kuehn,
I haven't tried this sollution on chinese characters since I can't
reproduce them, but since there is a standard I think this sollution
will work.
Before you submit the form check the value of input or textarea with
a small javascript function.
Ex. form:
<form action="./" method="post" onSubmit="check();"
name="theForm">
<input type="text" name="toBeChecked">
<input type="submit">
</form>
Javascript function:
var check_ok = false;
function check() {
var test_value = document.theForm.toBeChecked.value;
test_value = escape( test_value );
if ( test_value.indexOf( "%" ) == -1 ) {
var check_ok = true;
} else {
var check_ok = false;
}
if ( check_ok ) {
document.theForm.submit();
}
}
This works for all "illegal" characters since they always get
encoded therefor I see no way that chinese characters will pass
thru this function.
Good luck!
HI
the above solution is also ok if you entering text does not have
space then encodeing is ok. other wise space will get encoded as %20
so the above may give an error.:-)
U can use another menthod like
usually whate ever the characters entered in other language's
characters ascii value will be more than 256 so. read the string and
loop though each character and check that value is greater than 256
then alert a message other wise continue looping u can have better
understanding if u go thorugh the below code
<body>
<textarea cols=20 rows=20 id=txtarea>
</textarea>
<input type=button value=click onclick="check()">
</body>
<script>
function check()
{
var str=new String()
str=document.getElementById("txtarea").value;
for(i=0;i<str.length;i++)
{if(str.charCodeAt(i)>256)
alert("Do not enter other language characters");
}
}
</script>