2012-11-06 44 views
4

可能重複:
How do I make a textbox that only accepts numbers?驗證一個文本框,只允許數值

我有,我想保存爲一個字符串的電話號碼。

我讀到這在使用

txtHomePhone.Text 

我想我需要的是某種形式的數字是,但不能得到它的工作

if (txtHomePhone.Text == //something.IsNumeric) 
{ 
    //Display error 
} 
else 
{ 
    //Carry on with the rest of program ie. Add Phone number to program. 
} 

什麼是隻允許數字的最佳方式要輸入的值?

+0

'txtHomePhone'代表一個'TextBox'嗎?如果是這樣,你可以使用'KeyPress'事件來接受你想要允許的字符,並拒絕你不想在'TextBox'中輸入的內容。有一個美好的一天:) –

+0

它確實代表一個文本框,我如何找出哪個按鍵是哪個? – Wizard

+0

這是一個網絡或客戶端應用程序? –

回答

8

由於txtHomePhone表示TextBox,您可以使用KeyPress事件接受的人物,你想允許和拒絕你不喜歡什麼,允許在txtHomePhone

public Form1() 
{ 
    InitializeComponent(); 
    txtHomePhone.KeyPress += new KeyPressEventHandler(txtHomePhone_KeyPress); 
} 
private void txtHomePhone_KeyPress(object sender, KeyPressEventArgs e) 
{ 
    if (e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == '') //The character represents a backspace 
    { 
     e.Handled = false; //Do not reject the input 
    } 
    else 
    { 
     e.Handled = true; //Reject the input 
    } 
} 

提示The following character (which is not visible) represents a backspace.
注意事項:您可以始終允許或禁止使用e.Handled的特定字符。
注意:您可以創建一個條件語句,如果你想使用-()只有一次。如果您希望允許這些字符輸入特定位置,我建議您使用正則表達式。

if (e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == '') //The character represents a backspace 
{ 
    e.Handled = false; //Do not reject the input 
} 
else 
{ 
    if (e.KeyChar == ')' && !txtHomePhone.Text.Contains(")")) 
    { 
     e.Handled = false; //Do not reject the input 
    } 
    else if (e.KeyChar == '(' && !txtHomePhone.Text.Contains("(")) 
    { 
     e.Handled = false; //Do not reject the input 
    } 
    else if (e.KeyChar == '-' && !textBox1.Text.Contains("-")) 
    { 
     e.Handled = false; //Do not reject the input 
    } 
    else if (e.KeyChar == ' ' && !txtHomePhone.Text.Contains(" ")) 
    { 
     e.Handled = false; //Do not reject the input 
    } 
    else 
    { 
     e.Handled = true; 
    } 
} 

謝謝,
我希望對您有所幫助:)

3

試試這個

if (!txtHomePhone.Text.All(c=> Char.IsNumber(c))) 
{ 
    //Display error 
} 
else 
{ 
    //Carry on with the rest of program ie. Add Phone number to program. 
} 
+1

這是做什麼的?它給你一個真/假的價值,但不給任何真正的驗證。 (另外,對於原始問題中使用的電話號碼示例,這可能不夠:它會針對可能在典型電話號碼輸入中的符號和空白返回錯誤。) –

+1

@DanPuzey在他的例子中,他特別寫了:如果(txtHomePhone.Text == ** // something.IsNumeric **)。所以我想有沒有whitspaces等。關於驗證,他所要做的就是檢查isNumeric。我將代碼添加到if語句中以更好地解釋。 – Blachshma

3

要檢查,看是否已經輸入一個數值,您可以使用Integer.TryParse

int num; 
bool isNum = Integer.TryParse(txtHomePhone.Text.Trim(), out num); 

if (!isNum) 
    //Display error 
else 
    //Carry on with the rest of program ie. Add Phone number to program. 

但要記住電話號碼不一定是唯一的數字。請參閱Trevor Pilley回答掩蓋的文本框。

7

我假設你在這裏使用Windows窗體,看看MaskedTextBox。它允許你指定一個字符的輸入掩碼。

txtHomePhone.Mask = "##### ### ###"; 

由於這允許您限制輸入值,因此可以安全地將值解析爲整數。

注意:如果您使用的是WPF,我並不認爲基礎庫中有MaskedTextBox,但NuGet上有可用的擴展,它們可能會提供類似的功能。

+0

他正在尋找一種方法來驗證數字是數字,而不是隱藏在那裏。 – MyCodeSucks

+1

掩碼指定允許的字符,它不會像密碼輸入掩碼一樣隱藏輸入。 –

+1

@TyrionLannister Maskedtextbox不會隱藏它強制輸入符合「掩碼」的值。所以在Trevor的例子中,文本框將只顯示格式爲「##### ### ###」'的11個數字。空間被迫在那裏。 – Brad

0

通常我會使用Integer.TryParse作爲推薦的davenewza,但另一種選擇是使用C#中的VisualBasic IsNumeric函數。

添加對Microsoft.VisualBasic.dll文件的引用,然後使用以下代碼。

if (Microsoft.VisualBasic.Information.IsNumeric(txtHomePhone.Text)) 
{ 
    //Display error 
} 
else 
{ 
    //Carry on with the rest of program ie. Add Phone number to program. 
} 
0

我發現這樣做是隻允許用戶輸入數字的最佳方式鍵入文本框,感謝您的幫助。

+0

我很高興你解決了你的問題。 [請注意,您可以將帖子標記爲表示您已解決問題的答案](http://i.stack.imgur.com/uqJeW.png)。祝你有美好的一天 :) –

相關問題