2
I’m making an application C# in Windows Form using .Net Framework 3.5. In this application I have a textbox for the input of CPF or CNPJ.
I need this field to have only CPF/CNPJ numbers and characters (./-). I would like to validate this field in the event Keypress using Regex. Would it be possible?
Omni, I need it to be every key pressed, so I need it to be the Keypress event.
– cumpadi
@andreguilherme can I ask why it has to be every key press? Ever thought about using a Maskedtextbox?
– Omni
Boss wants it that way :(
– cumpadi
@andreguilherme it is not for me to opine on the decisions made without knowing the reasons. Anyway can validate the field in the same
KeyPress
, but it will not prevent the user from leaving the field for example (at least without additional logic). And as I mentioned, running a regex every key costs. Just a detail that is not clear in your question, also wants to know how to do the Regex?– Omni