Writing a dynamic code which you don't need to write the same code for each input

Writing a dynamic code which you don't need to write the same code for each input

Hi,

Whit this code I can check if an text-input is empty or not and add an error message and some accessibility attributes to it if it's empty.


Now how can I make it dynamic, so I don't have to repeat the same code (almost the same code) for each input?

Thanks