I am trying to get phone number as input from user in textbox (I am using Business Skin Forms TbsSkinEdit
). Textbox should allow only numbers, spaces, dash and round brackets. Is there any business skin form control which can solve this problem? Or is there any other way to achieve this?
Numbers can be in below format, format verification is not requirement, only allow specified characters in input -
(223)2312-1323
23324-3423-2342
23123123
2234 2343
I tried by overriding KeyPress event, but with that I am able to allow only numeric characters in textbox, but other operations like Paste and Ctrl+V needs additional handling. So checking if there is any ready control available.
You can probably use the TbsSkinMaskEdit
component and set its EditMask
property. It has some sample masks to choose from and one of the sample masks is for phone numbers:
!\(999\)000-0000;1;_
... but you may edit the mask yourself and set it to this for example:
!\(999\)0000-0000;1;_
If that doesn't cut it, you could create an OnChange
event handler:
void __fastcall TForm1::bsSkinEdit1Change(TObject *Sender)
{
TbsSkinEdit& se = *static_cast<TbsSkinEdit*>(Sender);
decltype(se.Text) result;
bool update = false;
auto old_pos = se.SelStart - 1;
for(auto ch : se.Text) {
// numbers, spaces, dash and round brackets
if((ch >= '0' && ch <= '9') || ch == ' ' || ch == '-' || ch == '(' || ch == ')') {
result += ch;
} else {
update = true;
}
}
if(update) {
se.Text = result;
se.SelStart = old_pos;
}
}