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.

[173 byte] By [Neo_Donalda] at [2007-11-27 11:44:23]
# 1

http://www.google.com/search?q=validate+web-form+entry

ChuckBinga at 2007-7-29 17:55:28 > top of Java-index,Java Essentials,Java Programming...
# 2

If you provide a more specifc problem statement it's likely that you can get a more specific response.

ChuckBinga at 2007-7-29 17:55:28 > top of Java-index,Java Essentials,Java Programming...
# 3

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?

DrClapa at 2007-7-29 17:55:28 > top of Java-index,Java Essentials,Java Programming...
# 4

> However doing that might annoy your users.

They may even call you a bad man racist.

BigDaddyLoveHandlesa at 2007-7-29 17:55:28 > top of Java-index,Java Essentials,Java Programming...
# 5

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.

Neo_Donalda at 2007-7-29 17:55:28 > top of Java-index,Java Essentials,Java Programming...
# 6

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.

Neo_Donalda at 2007-7-29 17:55:28 > top of Java-index,Java Essentials,Java Programming...
# 7

(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.

DrClapa at 2007-7-29 17:55:28 > top of Java-index,Java Essentials,Java Programming...
# 8

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?

Neo_Donalda at 2007-7-29 17:55:28 > top of Java-index,Java Essentials,Java Programming...
# 9

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

hunter9000a at 2007-7-29 17:55:28 > top of Java-index,Java Essentials,Java Programming...
# 10

> Unfortunately only english characters it is a

> Business requirement, which I have no control over.

I think they're based on a nave clich.

CeciNEstPasUnProgrammeura at 2007-7-29 17:55:28 > top of Java-index,Java Essentials,Java Programming...
# 11

Megnzed a blyeggyjtemnyem? Egy darabos szibarackos joghurtot krek.

BigDaddyLoveHandlesa at 2007-7-29 17:55:28 > top of Java-index,Java Essentials,Java Programming...
# 12

> I think they're based on a nave clich.

Touch

petes1234a at 2007-7-29 17:55:28 > top of Java-index,Java Essentials,Java Programming...
# 13

> > I think they're based on a nave clich.

>

> Touch

I'm picking up a soupon of irony.

BigDaddyLoveHandlesa at 2007-7-29 17:55:28 > top of Java-index,Java Essentials,Java Programming...
# 14

You'd thing someone was cordinating this, eh?

BigDaddyLoveHandlesa at 2007-7-29 17:55:28 > top of Java-index,Java Essentials,Java Programming...
# 15

To me, it all seems so blas.

petes1234a at 2007-7-29 17:55:33 > top of Java-index,Java Essentials,Java Programming...
# 16

Time to deliver the coup de grce?

BigDaddyLoveHandlesa at 2007-7-29 17:55:33 > top of Java-index,Java Essentials,Java Programming...