type restrict characters <>": in a webtextfield in java

Hi to all,
I appreciate if someone writes down the java script code for preventing the user to type ‘<’, ‘>’, ‘"’,’:’ in a webtextfield… or any equivalent in xojo code?

Also, preventing the copy function from a htmlviewer (maybe by not allowing highlighting text, or left mouse click and press shift)?

Thanks in advanced
regards
mb

I posted such a JavaScript in
https://forum.xojo.com/27366-filtering-webtextfield-input/0

You will have to modify it slightly.

Thanks, Michel,
Actually I have found some of your old post
https://forum.xojo.com/39730-restrict-entry-in-webtextfield-to-numbers

self.ExecuteJavaScript("document.getElementById('"+me.controlId+"').setAttribute('onkeydown','" +_ "if (event.keyCode < 48 || event.keyCode > 57 && event.keyCode != 44 && event.keyCode != 46 " + _ "&& event.keyCode != 188 && event.keyCode != 190) {event.preventDefault()}');")

but have trouble to convert it despite the fact that I have substitute with ascii 60,62,34,72
I think that problem is that I want just these particular characters and not to restrict all numbers or letters.
After that I decide to leave this post for getting it right.

Hi Micheal,

Here is code that will prevent < > " and : from being entered. I placed it in the Shown event of the WebTextField.

self.ExecuteJavaScript("document.getElementById('"+me.controlId+"_inner').setAttribute('onkeydown','" +_ "if (event.keyCode == 188 || event.keyCode == 190 || event.keyCode == 51 || event.keyCode == 186) " +_ "{event.preventDefault()}');")

I am doing this on Mac, and have no time to check that on an extended PC keyword. it is possible < and > may have other codes.

If that was the case, you can see which codes are generated with this:

self.ExecuteJavaScript("document.getElementById('"+me.controlId+"_inner').setAttribute('onkeydown','" +_ "console.log(event.keyCode);')")

Open the Developers tools of the browser, and select console. Then type the character you want to know the code from.

In the code above, == means equal to, and the double vertical bar || mean or. It will be easy to add more codes to exclude.

Thanks Michel, i appreciate your support!
I am aware of || and == but somehow I had trouble to make the code of the first post to work…
I thought that ascii table was universal regardless OS…

Keycodes are not the same as ascii. They overlap for numbers and letters (lowercase only, no uppercase) but keycodes represent the keys on the keyboard, not the character they produce. There are keycodes for the shift key, etc.

That said, keycodes should be OS agnostic.

You mean I need something like this?
http://www.classicteck.com/rbarticles/mackeyboard.php

ps.found this:
http://www.javascripter.net/faq/keycodes.htm
In Michel’s example 51 should be substitute by 222 while should be added the keycode 59 for firexox users

However it restricts not only
< but also ,

but also .
: but also ;
" but also ’
as it restrict the keyboard button corresponding to these keycodes

What a mess! Why is so difficult to implement keycode=0 as in desktop xojo versions keypressed event ???

If I got a bit of time, I will get back to that. Indeed keycode will restrict the entire key, upper and lowercase. I will see if it is possible to get the character instead.

There. This routine compares with the character, and not the keyCode.

self.ExecuteJavaScript("document.getElementById('"+me.controlId+"_inner').setAttribute('onkeydown','" +_ "if (event.key == ""<"" || event.key == "">"" || event.key == String.fromCharCode(34) || event.key == "";"") " +_ "{event.preventDefault();}');")

Thanks Michel so much, this is the right code!
Was so simple…
I will take a 48hours course for JavaScript next month, just for doing these simple things :slight_smile:
Also, I’m thinking about a similar course in Python.
Regards
MB

IMHO, JavaScript is critical, if you are serious about Xojo Web development.