There are many validation pages that you can read if you need them for reference in the future.
https://support.wix.com/en/article/working-with-user-input-validation-in-the-settings-panel
https://www.wix.com/corvid/example/custom-validations
https://support.wix.com/en/article/corvid-about-validating-user-input-with-code
For code reference see here.
https://www.wix.com/corvid/reference/$w.ValidatableMixin.html
You can also do the validation on the text input itself.
https://www.wix.com/corvid/reference/$w.TextInput.html
https://www.wix.com/corvid/reference/$w.TextBox.html
Finally, if you are using onKeyPress, then you should look at giving the text a little bit of a timeout so that the text is fully entered before anything else happens.
https://www.wix.com/corvid/forum/corvid-tips-and-updates/give-the-textinput-onkeypress-function-some-time