Prevent Non-English Characters on JSP forms
I was hoping to get any programming tips/ideas to prevent users from entering non-english text on web-forms.
Any feedback would be greatly appreciated. Thanks.
I was hoping to get any programming tips/ideas to prevent users from entering non-english text on web-forms.
Any feedback would be greatly appreciated. Thanks.
If you provide a more specifc problem statement it's likely that you can get a more specific response.
That in itself isn't a Java Programming question because Java has nothing to do with HTML and web forms.
You might be able to do that with Javascript (which as you may know isn't Java).
However doing that might annoy your users. Perhaps you should fix up your back-end application so the question goes away?
I have a jsp page something like:
--
<tr>
<td colspan=2> </td>
<td colspan=2>
<textarea name="title" cols="<%=cols%>" rows="3" wrap><%= form.getTitle()%></textarea>
</td>
</tr>
--
When the user submits the page, I do the form validation in the java formhandler. I was hoping that I could somehow compare the ascii codes of the character to ensure user is entering only english characters.
The following is the code, I have written in java the form-handler
-
for (int i =0 ; i < title.length() ; i++) {
char c = title.charAt(i);
System.out.println("c = " + c + ", ascii = " +(int)'c');
if (int(c) > 127) {
setErrorMessage(ID.QUESTION.TITLE, "Non-English characters are not allowed. Please enter the required information only in Enlgish.");
}
}
-
But for some reason which I am not able to debug, is that no matter what character I enter english or non-english its ascii equivalent i.e. the int(c) value getting printed out is always 99. Moreover even if I enter a non-english character, in the system.out it is printing its english equivalent...if that makes any sense...
I hope I was able to explain my problem...Any help/feedback would be greatly appreciated.
Thanks.
I have a jsp page something like:
--
<tr>
<td colspan=2> </td>
<td colspan=2>
<textarea name="title" cols="<%=cols%>" rows="3" wrap><%= form.getTitle()%></textarea>
</td>
</tr>
--
When the user submits the page, I do the form validation in the java formhandler. I was hoping that I could somehow compare the ascii codes of the character to ensure user is entering only english characters.
The following is the code, I have written in java the form-handler
-
for (int i =0 ; i < title.length() ; i++) {
char c = title.charAt(i);
System.out.println("c = " + c + ", ascii = " +(int)'c');
if (int(c) > 127) {
setErrorMessage(ID.QUESTION.TITLE, "Non-English characters are not allowed. Please enter the required information only in Enlgish.");
}
}
-
But for some reason which I am not able to debug, is that no matter what character I enter english or non-english its ascii equivalent i.e. the int(c) value getting printed out is always 99. Moreover even if I enter a non-english character, in the system.out it is printing its english equivalent...if that makes any sense...
I hope I was able to explain my problem...Any help/feedback would be greatly appreciated.
Thanks.
(int)'c'
Returns the value of the character 'c' converted to an int. That's 99.(int)c
Returns the value of the variable c converted to an int.
Personally I think your "Speak English" message is rude and insulting, and I would be extremely annoyed if I got it after keying in an English word that contains an accented character. I still think you should fix your back end software so you don't have to do this.
Thanks. That helped. Your suggestion help me address one issue.
Unfortunately only english characters it is a Business requirement, which I have no control over.
The other issue I had was, I was entering non-english character on the page such as '', but when I broke down the characters using the above for-loop it printed them out as follows:
c = &, ascii = 38
c = #, ascii = 35
c = 2, ascii = 50
c = 5, ascii = 53
c = 1, ascii = 49
c = 6, ascii = 54
c = 3, ascii = 51
c = ;, ascii = 59
Which I am not sure why. Any thoughts?
That's an html escape sequence. Some encodings don't support all characters, so they get encoded as &# followed by a hex number, then ;.
http://en.wikipedia.org/wiki/Html#Other_markup
http://www.w3.org/TR/html4/charset.html#entities
Message was edited by:
hunter9000
> Unfortunately only english characters it is a
> Business requirement, which I have no control over.
I think they're based on a nave clich.