I posted that in the Italian channel a couple weeks ago :
[code]Sub Shown() Handles Shown
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()}’);”)
End Sub[/code]
Contrary to the default number field, this supports both comma decimal separator, and dot. Important in Europe.
If the dot is not necessary, remove the test for keyCode 44 and 190.
Ben_Ted
(Ben Ted)
March 30, 2017, 8:17am
2
Thanks a lot, this feature is perfect for me.
[quote=324582:@Ben Ted]can you help me? for the conversation to Restrict TextField to Numbers?
It is beautiful for my project but it will freeze me the numbers of the right of the keyboard.
it’s a italian keyboard.
there is a way to enable them?[/quote]
Here is a revised code that enables arrow keys left and right, as well as delete and backspace.
self.ExecuteJavaScript("document.getElementById('"+me.controlId+"').setAttribute('onkeydown','" +_
"console.log(event.keyCode); if ((event.keyCode < 48 || event.keyCode > 57) && event.keyCode != 44 && event.keyCode != 46 " + _
"&& event.keyCode != 188 && event.keyCode != 190 && event.keyCode != 8 && event.keyCode != 37 && " + _
"event.keyCode != 39) " + _
"{event.preventDefault()}');")
Hope this helps.
Ben_Ted
(Ben Ted)
April 5, 2017, 9:14am
4
does not work, the numbers are in the right of the keyboard do not work.
That’s because the keypad numbers have different keycodes.
I found this resource during my research for an internal subclass that we use, it may be of guidance.
https://github.com/lockevn/html-numeric-input
Ah, OK. I see what it is. Will be fixed soon. I did not understand “freeze me the numbers of the right of the keyboard”, thought it was no backspace.
self.ExecuteJavaScript("document.getElementById('"+me.controlId+"').setAttribute('onkeydown','" +_
"console.log(event.keyCode); if ((event.keyCode < 48 || event.keyCode > 57) && event.keyCode != 44 && event.keyCode != 46 " + _
"&& event.keyCode != 188 && event.keyCode != 190 && event.keyCode != 8 && event.keyCode != 37 && " + _
"event.keyCode != 39 && event.keyCode != 96 && event.keyCode != 97 & event.keyCode != 98 &&" + _
"event.keyCode != 99 && event.keyCode != 100 && event.keyCode != 101 & event.keyCode != 102 &&" + _
"event.keyCode != 103 && event.keyCode != 104 && event.keyCode != 105 && " + _
"event.keyCode != 188) " + _
"event.keyCode != 109 && event.keyCode != 111 && event.keyCode != 110 &&" + _
"event.keyCode != 107 && event.keyCode != 190 && event.keyCode != 107 &&" + _
"{event.preventDefault()}');")
I also enabled /*-+. from the numeric keypad. To disable them, delete the two lines before last.
Ben_Ted
(Ben Ted)
April 6, 2017, 7:13am
7
I solved this way :
self.ExecuteJavaScript(“document.getElementById(’”+me.controlId+"’).setAttribute(‘onkeydown’,’" +_
“if (event.keyCode >57 && event.keyCode < 93 || event.keyCode >105 && event.keyCode!= 109 && event.keyCode!= 188 ) {event.preventDefault()}’);”)
Congratulations. You learned how to fish