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

  1. 2 weeks ago
    Edited 2 weeks ago

    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

    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.

  2. Michel B

    Oct 6 Pre-Release Testers, Xojo Pro RubberViews.com

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

    You will have to modify it slightly.

  3. Edited 2 weeks ago

    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.

  4. last week

    Michel B

    Oct 7 Pre-Release Testers, Xojo Pro Answer RubberViews.com
    Edited last week

    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.

  5. 2 days ago
    Edited 2 days ago

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

  6. Tim H

    Oct 15 Pre-Release Testers Portland, OR USA
    Edited 2 days ago

    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.

  7. 20 hours ago
    Edited 18 hours ago

    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 ???

  8. 15 hours ago

    Michel B

    15 hours ago Pre-Release Testers, Xojo Pro RubberViews.com

    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.

  9. 14 hours ago

    Michel B

    14 hours ago Pre-Release Testers, Xojo Pro RubberViews.com

    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();}');") 

or Sign Up to reply!