-1

In my web form I have 2 field that should only accept number (the calculation result of their value should shown in the third field. The RegularValidation of those two fields works fine before user click submit button. but NOT after. How to handle it?

    <asp:TextBox ID="TextBox1" runat="server" placeholder="Liter"></asp:TextBox><span style="color:red;font-weight:bold"> *</span>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="This is required" ForeColor="Red"  ValidationGroup="test" ControlToValidate="TextBox1" Display="Dynamic"></asp:RequiredFieldValidator>
    <asp:RegularExpressionValidator ID="RegularExpressionValidator2" ControlToValidate="TextBox1" runat="server" ForeColor="red" ErrorMessage="Enter only numbers!" ValidationExpression="\d+" Display ="Dynamic"></asp:RegularExpressionValidator>

The submit button:

 protected void btn_Click(object sender, EventArgs e)
{
    if (Page.IsValid)
    {
           TextBox3.Text = ((Convert.ToInt32(TextBox1.Text) * 4.18 * Convert.ToInt32(TextBox2.Text)) / 3600).ToString();
           double result = Convert.ToDouble(TextBox3.Text);
           TextBox3.Text = String.Format("{0:0.00}", result);
Biju
  • 71
  • 1
  • 2
  • 8

2 Answers2

0

you can write a function / method like this to pass the value of the TextBox

public static bool isNumeric(string val, System.Globalization.NumberStyles NumberStyle)
{
    Int32 result;
    return Int32.TryParse(val, NumberStyle,
        System.Globalization.CultureInfo.CurrentCulture, out result);
}

Call and check the method for example passing the following

var _isNumeric2 = isNumeric("9.", System.Globalization.NumberStyles.Integer);

Replace the first paramtere with the TextBox3.Text value

MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • Thanks @MethodMan, but the TextBox1 and TextBox2 shall be checked! – Biju Aug 17 '16 at 16:14
  • then make the check / call appropriate there are several ways to handle this if this works then mark it as the acceptable answer.. you could change the method to work with any text value – MethodMan Aug 17 '16 at 16:16
0

All was needed is to add ValidationGroup="test" same as the field. The solution is:

    <asp:TextBox ID="TextBox1" runat="server" placeholder="Liter"></asp:TextBox><span style="color:red;font-weight:bold"> *</span>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="This is required" ForeColor="Red"  ValidationGroup="test" ControlToValidate="TextBox1" Display="Dynamic"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" ControlToValidate="TextBox1" runat="server" ForeColor="red" ErrorMessage="Enter only numbers!" ValidationExpression="\d+" ValidationGroup="test" Display ="Dynamic"></asp:RegularExpressionValidator>
Biju
  • 71
  • 1
  • 2
  • 8