Key Press Event is not working properly and InputType automatically switch From Text to Numeric whenever KeyPress event started

Answered Closed
0
0

Hello Wisej Team,

I am working on Keypress Event for users to be only allowed to input numeric value but the Keypress event behaviour is not working as it should be. The same event and code are working perfectly fine in Winforms, as I have tested theirs.

Furthermore, I know I can use InputType property as Numeric but I also have some field where I want to allow decimal and comma, so this event is necessary for that case as well. Which brings to my second issue; whenever I tried for KeyPress event, for some reason the InputType automatically change from Text to Numeric. I have no idea why this happens.

Kindly below find the event and code which I am using.

private void General_Numeric_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
{
e.Handled = true;
}

}

Thank you very much in advance for your assistance.

  • You must to post comments
Best Answer
0
0

You are not on winforms, the browser is on the client machine. When the KeyDown event is fired on the server it has already happened. It is impossible with any technology to control the user’s keyboard from the server. The only way would be a synchronous event (which is deprecated) making the browser unusable. Setting e.Handled = true can only be used by the server code.

If you want to change the keyboard behavior of the client browser you have to do it on a client event or by using a regexp filter:

Client Event

In the designer add a ClientEvent “keydown” and in the event code put:

if (isNaN(e.getKeyIdentifier()))
  e.preventDefault();

There are a million ways in JavaScript to check the typed character.

Or in code use

this.textBox1.AddClientEventListener(“keydown”, “if (isNaN(e.getKeyIdentifier())) e.preventDefault();”);

 

Filter

Set the Filter property to “[0-9]”. It’s a regular expression. You can test it with this https://regex101.com/

InputType

Cannot reproduce. I set it to Number and attached KeyPress, worked fine. Can you send a text case? Also note that InputType is a native feature of the browser and not something implemented in Wisej: https://www.w3schools.com/html/html_form_input_types.asp

  • Ammad Ahmed
    Thank you very much for your reply Luca. I am really sorry, I forgot to mention that I am working Wisej Web Desktop Application. I am not sure, how can I add the JS snippet in that?
  • Luca (ITG)
    It’s the same. It’s still a web application.
  • Ammad Ahmed
    Thanks a lot for the assistance, your Filter solution works best. The NaN solution was only allowing Number and no backspace was working to delete the existing string.
  • You must to post comments
Showing 1 result