Click here to Skip to main content
15,887,874 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I selected a textbox and want to enter only integer value.
Eg like a phone number.

How could I do that.

kudos
neaS
Posted
Updated 20-Oct-11 23:15pm
v2
Comments
BobJanova 21-Oct-11 5:33am    
WinForms? WPF? Web?

Also, phone numbers are, in general, not 'integers'. They are strings which consist mostly of numbers but also other characters. For example: "01234 567890" or "+353 1 2345 6789". Even removing the other characters, in many countries the national version of any number starts with '0' which will be lost if you represent it as a number.

(As an aside, I wonder how many calls that number gets. It is a valid Bedford number, though I suppose it probably isn't allocated.)

You can use javascript for that

try this
JavaScript
function checkIt(evt) {
    evt = (evt) ? evt : window.event
    var charCode = (evt.which) ? evt.which : evt.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        status = "This field accepts numbers only."
        return false
    }
    status = ""
    return true
}
 
Share this answer
 
v2
By using javascript you can do it

function NumOnly(ctrl) {
            chr = document.getElementById(ctrl).value;

            var reg1 = /.*[a-zA-Z].*/;
            if (document.getElementById('<%=txtMobile.ClientID %>').value == "")
                return;
            if (reg1.test(chr)) {
                document.getElementById(ctrl).value = "";
                alert("Enter numbers only");
                document.getElementById(ctrl).focus();

            }

        }



XML
<asp:TextBox ID="txtMobile" runat="server" Width="159px" TabIndex="17" onkeypress='NumOnly(this.id)' MaxLength="10"></asp:TextBox>
 
Share this answer
 
v2
 
Share this answer
 
v2
Comments
Dalek Dave 21-Oct-11 5:16am    
Is how I would do it!
My advice would be to use a NumericUpDown control which only accepts integers. If you have to use a text box, then you are going to have to handle the KeyPress event and intercept any letters or synmbols entered. Something like this

C#
if(!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
              e.Handled = true;



Hope this helps
 
Share this answer
 
C#
public static void CheckNumeric(System.Windows.Forms.KeyPressEventArgs kv, Boolean myCurrency)
        {
            if (!myCurrency)
            {
                if ((kv.KeyChar < (char)48 || kv.KeyChar > (char)57) && (kv.KeyChar != (char)8))
                    kv.Handled = true;

            }
            else
            {
                if ((kv.KeyChar < (char)48 || kv.KeyChar > (char)57) && (kv.KeyChar != (char)8) && (kv.KeyChar != (char)46))
                    kv.Handled = true;
            }

        }


use this code in keypress event of textbox
 
Share this answer
 
v2
C#
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{

    if (this.textBox1.ReadOnly) return;

    if ((int)e.KeyChar < 32) return;

    if (!char.IsDigit(e.KeyChar) && (int)e.KeyChar != 46) //46=> .
    {
        e.Handled = true;
        return;
    }

}
 
Share this answer
 
Beginning with .NET 2.0, you have the MaskedTextBox Control which is ready-to-go for such tasks as entering phone numbers.

Drop one on a WinForm, and select its 'Mask property: open the pop-up window to select from a number of pre-defined templates.

In .NET 4.0, there are two phone-number options to choose from (I don't know what the options were in .NET 2.0).

For numeric entry, as mentioned by Wayne Gaylard above, use a NumericUpDownControl.

best, Bill
 
Share this answer
 

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