186
Views
12
Comments
JavaScript 'event' is deprecated.

In input we are using event.key. Now event is deprecated so what is the correct way to use OnChange event of input box. Also event.key will not work on mobile so to get value from the input box on mobile key board what is the best way ?

2024-01-04 15-15-51
Abed Al Banna

Hi @Navneet Garg 

Are you referring to this "OnChange" event? Could you perhaps elaborate more on what are you trying to achieve?

2022-12-30 07-28-09
Navneet Garg

Yes I want to use OnChange event to validate input field (for blocking special characters)

2024-01-04 15-15-51
Abed Al Banna

You can do so by adding a new screen action to handle the "On Change" event, and add to the flow something like this:

2024-01-04 15-15-51
Abed Al Banna

Seeing your other reply, you can modify the code to include the RegEx search:

2022-12-30 07-28-09
Navneet Garg

How to implement regex flag like "g and i" to check the repeated characters?

2024-01-04 15-15-51
Abed Al Banna

Please elaborate on what exactly do you want to achieve. You just want to check if a specific character is duplicated within a string, while ignoring the case-sensitivity? Some examples would be great.

2022-12-30 07-28-09
Navneet Garg

This is my javascript code. Now I want to remove special characters from the following text so my output will be "test123". I want to know that how we can pass flags like "gi" in the pattern?

let stringToReplace = "test#123#$%##@";

var desired = stringToReplace.replace(/[^\w\s]/gi, "")

console.log(desired) //test123


2024-01-04 15-15-51
Abed Al Banna

1. Add the "Regex_Replace" as a dependency from the Text extension:

2. Configure it as such:

Example:

2023-05-08 05-34-05
Piyali Saha

Hi Navneet Garg,

Are you talking about the events present there on input widget or any other JavaScript event. Please elaborate your query.

2022-12-30 07-28-09
Navneet Garg

Basically I am using text field OnChange event and in that action I am using a JavaScript to validate keyboard keys (like to block special keys #, $ etc.) 

something like this

now out system giving me waring that event' is deprecated which is true as javascript will remove the event. So now how to pass event in the OnChange ?

https://developer.mozilla.org/en-US/docs/Web/API/Window/event

var regex = new RegExp("[a-zA-Z0-9]+$");var key = event.key;if (!regex.test(key)) {    event.preventDefault();}


var event: Event

@deprecated

'event' is deprecated.(6385)lib.dom.d.ts(18286, 5): The declaration was marked as deprecated here.

No quick fixes available


One more problem is that event.key will not work on the mobile keyboard. So how we can validate input field on mobile as well ?

2023-05-08 05-34-05
Piyali Saha

You can use this javascript to prevent special characters in input field:

document.getElementById($parameters.WidgetId).addEventListener("keypress", function(e){    var patternNotOk = eval("^a-zA-Z0123456789]/g");   

 var text = this.value.replace(patternNotOk, '');    

if (this.value != text){        this.value = text;    }});

Try to use this JS and let me know if still the issue persists.

Thanks & Regards.

2022-12-30 07-28-09
Navneet Garg

Thanks for now I implemented similar kind of solution.

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.