2017-04-07 21 views
0

下面的代碼只允許輸入整數,但不是隻輸入整數,我需要給用戶輸入雙倍(0.00)的選項。任何建議或修改?如何在ASPX C#網站的文本框中將值接受爲雙打?

if (_txStaticPressureUpdate.Trim() == "") 
    { 

     txSystemMessage.Text = "Field 'Static Pressure' is empty. Please enter a valid value."; 
     txSystemMessage.ForeColor = System.Drawing.Color.Red; 
     return; 
    } 

    //check that record info entered consists of numbers only/no special characters or letters 
    for (int j = 0; j < _txStaticPressureUpdate.Length; j++) 
    { 
     if (!char.IsControl(_txStaticPressureUpdate[j])) 
     { 
      txSystemMessage.Text = "Field 'Static Pressure' has an invalid value."; 
      txSystemMessage.ForeColor = System.Drawing.Color.Red; 
      return; 
     } 
     else 
     { 
      staticPressure = double.Parse(txStaticPressureUpdate.Text.Trim()); 
     } 
    } 

我正在ASPX/C#網站上工作。許多可用的選項都是針對WinForms的。它可以起作用,但在公式的最終結果中更精確,我期待使用雙打。

回答

3

double.TryParse可用於檢查輸入的字符串是否爲double。它返回一個布爾值,指示解析是否成功,並將解析值傳遞給變量。

例如,

if (!double.TryParse(_txStaticPressureUpdate.Text.Trim(), out staticPressure)) 
    { 
     txSystemMessage.Text = "Field 'Static Pressure' has an invalid value."; 
     txSystemMessage.ForeColor = System.Drawing.Color.Red; 
     return; 
    } 

您將不需要遍歷並檢查單個字符。

+0

太棒了!非常感謝你,@James 太棒了!這絕對是完美的! –

相關問題