I’m trying to figure out a clean way of restricting BJS input text entry to decimal numbers (chars 0 to 9, decimal point and negative sign). The following regex expression works nicely in my Flutter interface: RegExp(r"^-?\d*.?\d{0,3}"))
How can I add the regular expression ‘^-?\d*.?\d{0,3}’ to a BJS InputText box?
It would be great if the BJS InputText box had a regex ‘pattern’ attribute
Thank you Roland. I don’t understand why it won’t work with my regex ^-?\d*.?\d{0,3} which should allow an optional minus, then digits, and optional decimal point and then digits to 3 decimal places. The regex works fine in Flutter and validates in online regex validators - any ideas why it doesn’t work here?
edit; you may want to group the decimal with the decimal places for a final verification as you want to allow it while typing - on key press can be restrictive for typing. typically a ‘blur’ event is easier for this kind of validation. agree validation would be helpful OOTB.
@rjt there is a clipboard observable, text changed observable. Look it up in the GUI docs / api. The first PG shows how to restrict text in a BabylonJS input. The second one offers an alternative. You can modify the condition to whatever you like. Sorry, I can’t help you with the regex itself, because I always search for it on the web when I need something. I am not experienced in this field.
If that is intended for me - I don’t think I would propose a solution as part of GUI. regex will only get you so far - If I was building an application that needed to restrict input then I would probably try to bring in a 3rd party form validation library and hook it into GUI events. Maybe somebody in the community has already done so and will share.