複製代碼 代碼如下:protected void txtQty_TextChanged(object sender, EventArgs e)
{
checkForm();
}
//檢驗文本資訊是否合法,如果合法則開始計算
protected void checkForm()
{
try
{
if (!IsNumberic(txtQty.Text) && txtQty.Text != "")
{
checkbool = false;
Response.Write("<script>alert('數量只能為數字,請輸入數字資訊,謝謝合作!')</script>");
txtQty.Text = "";
txtQty.Focus();
}
else if (txtQty.Text != "")
{
Qty = int.Parse(txtQty.Text);
}
if (!IsNumberic(txtVat.Text) && txtVat.Text != "")
{
Response.Write("<script>alert('稅額只能是數字,請輸入數字資訊,謝謝合作!')</script>");
checkbool = false;
txtVat.Text = "";
txtVat.Focus();
}
else if (txtVat.Text != "")
{
Vat = Double.Parse(txtVat.Text);
}
if (!IsNumberic(txtUnitPrice.Text) && txtUnitPrice.Text != "")
{
Response.Write("<script>alert('價格只能是數字,請輸入數字資訊,謝謝合作!')</script>");
checkbool = false;
txtUnitPrice.Text = "";
txtUnitPrice.Focus();
}
else if (txtUnitPrice.Text != "")
{
UnitPrice = Double.Parse(txtUnitPrice.Text);
}
if (checkbool == true)
{
if (Vat != 0 && ExVatAmount != 0)
{
AmountVat = ExVatAmount / (1 - Vat / 100);
txtAmountVat.Text = AmountVat.ToString();
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
/// <summary>
/// 名稱:IsNumberic
/// 功能:判斷輸入的是否是數字
/// 參數:string oText:源文本
/// 傳回值: bool true:是 false:否
/// </summary>
public bool IsNumberic(string oText)
{
try
{
//從字串到雙精確度值的轉換,字串轉換為Double,如果成功則返回為真,否則返回為假。
Double var1 = Convert.ToDouble(oText);
return true;
}
catch
{
return false;
}
}
}