Dovberman
asked on
Regular Expression Validator to Trap non-numeric entry.
<asp:TextBox ID="txtMinPrice" runat="server" Width="45px">
</asp:TextBox>
<asp:RegularExpressionVali dator ID="valMinPrice" runat="server"
ErrorMessage="The Minimum Price needs to be numeric. "
ValidationExpression="^\d{ 0,3}(\.\d{ 1,2})?$"
ControlToValidate="txtMinP rice" SetFocusOnError="true"
EnableClientScript="False" Display="Dynamic">
</asp:RegularExpressionVal idator>
Entered abc instead of the number 5.
non-numeric not trapped
What should I change?
Thanks,
</asp:TextBox>
<asp:RegularExpressionVali
ErrorMessage="The Minimum Price needs to be numeric. "
ValidationExpression="^\d{
ControlToValidate="txtMinP
EnableClientScript="False"
</asp:RegularExpressionVal
Entered abc instead of the number 5.
non-numeric not trapped
What should I change?
Thanks,
ValidationExpression="^\d+ $"
ASKER
That did not work.
Any other suggestions?
Thanks,
Any other suggestions?
Thanks,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
To BuggyCoder:
<asp:TextBox ID="txtMinPrice" runat="server" Width="45px"
onTextChanged = "txtMinPrice_TextChanged" CausesValidation="true">
</asp:TextBox>
<asp:RegularExpressionVali dator ID="valMinPrice" runat="server"
ErrorMessage="The Minimum Price needs to be numeric. "
ControlToValidate="txtMinP rice" SetFocusOnError="true"
EnableClientScript="False" Display="Dynamic"
ValidationExpression="^\d+ $">
</asp:RegularExpressionVal idator>
protected void txtMinPrice_TextChanged(ob ject sender, EventArgs e)
{
int Test1 = 0;
if (this.IsValid)
{
Test1 = 1;
}
}
Error:
Page.IsValid cannot be called before validation has taken place.
It should be queried in the event handler for a control that has
CausesValidation=True and initiated the postback,
or after a call to Page.Validate.
<asp:TextBox ID="txtMinPrice" runat="server" Width="45px"
onTextChanged = "txtMinPrice_TextChanged" CausesValidation="true">
</asp:TextBox>
<asp:RegularExpressionVali
ErrorMessage="The Minimum Price needs to be numeric. "
ControlToValidate="txtMinP
EnableClientScript="False"
ValidationExpression="^\d+
</asp:RegularExpressionVal
protected void txtMinPrice_TextChanged(ob
{
int Test1 = 0;
if (this.IsValid)
{
Test1 = 1;
}
}
Error:
Page.IsValid cannot be called before validation has taken place.
It should be queried in the event handler for a control that has
CausesValidation=True and initiated the postback,
or after a call to Page.Validate.
you are applying it in text changed event of text box, this event will fire with every change in textbox data.
Ideally IsValid should be called on button click where we like to validate data before the final submit....
In your case you are making your text box to cause validation, this is not right or a good practice.
Suggest have a button and make its cause validation true and remove causevalidation from textbox....
Ideally IsValid should be called on button click where we like to validate data before the final submit....
In your case you are making your text box to cause validation, this is not right or a good practice.
Suggest have a button and make its cause validation true and remove causevalidation from textbox....
ASKER
To BasicInstinct
<asp:RangeValidator ID="valMinPrice" runat="server"
ErrorMessage="The Minimum Price needs to be numeric and between 1 and 100"
MaximumValue="100"
MinimumValue="1"
ControlToValidate="txtMinP rice" Display="Dynamic" SetFocusOnError="True">
</asp:RangeValidator>
Error on any value entered except "1.00" or "1".
<asp:RangeValidator ID="valMinPrice" runat="server"
ErrorMessage="The Minimum Price needs to be numeric and between 1 and 100"
MaximumValue="100"
MinimumValue="1"
ControlToValidate="txtMinP
</asp:RangeValidator>
Error on any value entered except "1.00" or "1".
ASKER
This is what I did that works. It is complex, but does the job.
I used the TextChanged event. Then disabled the main process button if the entry was not numeric.
Based on
https://www.experts-exchange.com/questions/22806510/IsNumeric.html
<asp:TextBox ID="txtMinPrice" runat="server" Width="45px"
onTextChanged = "txtMinPrice_TextChanged" AutoPostBack="True">
</asp:TextBox>
Code behind:
public void txtMinPrice_TextChanged(ob ject sender, EventArgs e)
{
lblResult.Text = "";
string strEntered = txtMinPrice.Text;
bool blnIsNumeric = IsNumeric(strEntered);
if (!blnIsNumeric)
{
lblResult.Text = "Price must be numeric and between 1.00 and 100.00";
cmdGetResults.Enabled = false;
}
else
{
cmdGetResults.Enabled =true;
}
return;
}
public static bool IsNumeric(string pstrEntry)
{
bool isNum;
double retNum;
isNum = Double.TryParse(Convert.To String(pst rEntry),
System.Globalization.Numbe rStyles.An y,
System.Globalization.Numbe rFormatInf o.Invarian tInfo, out retNum);
return isNum;
}
I used the TextChanged event. Then disabled the main process button if the entry was not numeric.
Based on
https://www.experts-exchange.com/questions/22806510/IsNumeric.html
<asp:TextBox ID="txtMinPrice" runat="server" Width="45px"
onTextChanged = "txtMinPrice_TextChanged" AutoPostBack="True">
</asp:TextBox>
Code behind:
public void txtMinPrice_TextChanged(ob
{
lblResult.Text = "";
string strEntered = txtMinPrice.Text;
bool blnIsNumeric = IsNumeric(strEntered);
if (!blnIsNumeric)
{
lblResult.Text = "Price must be numeric and between 1.00 and 100.00";
cmdGetResults.Enabled = false;
}
else
{
cmdGetResults.Enabled =true;
}
return;
}
public static bool IsNumeric(string pstrEntry)
{
bool isNum;
double retNum;
isNum = Double.TryParse(Convert.To
System.Globalization.Numbe
System.Globalization.Numbe
return isNum;
}
ASKER
I appreciate the suggestions made and the effort spent.<br /><br />That's why I divided the points evenly.<br /><br />Thanks,