Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi


How can i allow to enter only 12 numbers into textbox in c# windows form
Posted
Updated 21-Aug-22 19:29pm

The simplest answer is to set the MaxLength property to 12.

There are ways around that simple setting with copy-paste etc. If the MaxLength is all you need then just accept the solution. If you need to deal with the other issues too, then comment below and I'll add to this solution.

Hope that helps ^_^
Andy

UPDATE: I have learnt from comments that the OP requires max of 12 numbers to be entered (thx Maciej Los ^_^):

If you want the field to be numeric only then there are ways to do this too:

C#
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    //We only want to allow numeric style chars
    if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) &&
        (e.KeyChar != '.'))
    {
            //Setting e.Handled cancels the keypress event, so the key is not entered
            e.Handled = true;
    }

    // we only allow one decimal point here
    if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
    {
        e.Handled = true;
    }
    //keypress occurs before the text is updated.  Therefore sender.Text excludes the current key e.KeyChar 
}



Combine this with the MaxLength and you will have a numeric field 12 chars long.


If you want a textfield for x number of chars that may contain a max on 12 digits, then you can again use the keypress event, just don't set MaxLength:

C#
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    // same as testing for decimal above, we can check the text for digits
    if (char.IsDigit(e.KeyChar))
    {
        //Count the digits already in the text.  I'm using linq:
        if((sender as TextBox).Text.Count(Char.IsDigit)>=12)
            e.Handled = true;
    }
}


I hope this solution better suites your needs ^_^


UPDATE from OPs comments:
OP is using a RadMaskedEditBox from the Telerik suite.

The RadMaskedEditBox has it's own handlers for restricting input. These are called Masks (hence radMASKEDeditbox ^_^)

If you use these then you do not require custom format events. The RadMaskedEditBox will do it for you.

You can find the properties in the controls Properties window, or you can add them on form load.

MaskType = Standard (from dropdown); //The controls default masking
Mask = "0000000000" (enter as text); // The controls mask pattern. This represents 12 digits

If you can't find these options in the properties window then add them on form load like this:

myRadMaskedEditBox.MaskType = MaskType.Standard;
myRadMaskedEditBox.Mask = "000000000000";

Hope that clears up the confusion ^_^
 
Share this answer
 
v3
Comments
Maciej Los 27-Aug-15 5:04am    
Andy, i posted the same solution and removed it, because OP wants to disallow to enter more then 12 numbers - not chars!
Andy Lanng 27-Aug-15 5:24am    
updated, thanks ^_^
Member 11889312 27-Aug-15 6:07am    
hi sir
I am unable to use it because mine is not textbox mine is radMaskedEditBox

Inorder to this can u help me.
Andy Lanng 27-Aug-15 6:24am    
Oh - just use masking:
radMaskedEditBox1.MaskType = MaskType.Standard;
radMaskedEditBox1.Mask = "000000000000";
Member 11889312 27-Aug-15 6:30am    
Private void radMaskedEditBox5PhoneNo_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != '.'))
{
e.Handled = true;
}
if (char.IsDigit(e.KeyChar))
{
radMaskedEditBox5PhoneNo.MaskType = MaskType.Standard;
radMaskedEditBox5PhoneNo.Mask = "000000000000";
e.Handled = true;
}
}
this what i have written but under the 2nd MaskType it shows Error "This does not exist".What can we do to solve this?
 
Share this answer
 
Comments
Andy Lanng 27-Aug-15 5:19am    
This is what I went with in the update ^_^
Handle the TextChanged event, and check it via a regex:
\d{1,12}
should do.
If it fails the regex, restore the previous version. If it passes, save it as the previous version.
When you restore, use the SelectionStart to ensure that the cursor doesn't move, by reading it before you set the text, and setting it back to that value afterwards.
 
Share this answer
 
Comments
Member 11889312 1-Sep-15 1:20am    
it's not working yet

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900