Click here to Skip to main content
14,694,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a web form that has textboxes on it. I have two different validators for each textbox. The first validator is a range validator and the second validator is a regularexpressionvalidator. I have the REV ValidationExpression set to ^[0-9]. When the user tests the textbox to see what will happen this error comes up:
Input string was not in a correct format.

The textbox does have a textbox text change on it with calculations. Is there a way to get the textbox to only allow numbers and not do any calculations unless there is a number in the textbox?

<asp:TextBox ID="TextBoxFTUG" runat="server" Width="180px" AutoPostBack="True" ontextchanged="TextBoxFTUG_TextChanged" 


<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 

                    ControlToValidate="TextBoxFTUG" Display="Dynamic" ErrorMessage="Numbers Only" 

                    style="color: #FF0000; font-weight: 700" ValidationExpression="^[0-9]"></asp:RegularExpressionValidator>

<asp:RangeValidator ID="RangeValidatorLYFTUG" runat="server" 

                    ControlToValidate="TextBoxFTUG" CssClass="style40" 

                    ErrorMessage="20% Difference. Please verify numbers" ForeColor="Red" 

                    Type="Integer" Enabled="True" Display="Dynamic"></asp:RangeValidator>

Here is where I get my error:
 Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: Input string was not in a correct format.

Source Error:

Line 204:    protected void TextBoxFTUG_TextChanged(object sender, EventArgs e)
Line 205:    {
Line 206:        int w = Convert.ToInt32(TextBoxFTUG.Text.Replace(",", ""));
Line 207:        int x = Convert.ToInt32(TextBoxFTG.Text.Replace(",", ""));
Line 208:        int c = Convert.ToInt32(TextBoxTHUGDR.Text.Replace(",", ""));

Source File: C:\Users\khopkins\Documents\Visual Studio 2010\Projects\SACSCOCLogin1.1\SACSCOCLogin1.1\FTEEnrollmentInformation.aspx.cs    Line: 206 
Updated 13-Nov-14 9:38am
PIEBALDconsult 13-Nov-14 14:16pm
Doesn't that check only the first character?
How about replacing [0-9] with \d ?
What is the input?
What are you really trying to accomplish? I suspect the problem isn't with the Regular Expression, but something else, that looks like the error you get when you try to Parse a bad string into an integer.
Computer Wiz99 13-Nov-14 14:26pm
I tried that expression also and get the same error. What I am trying to do is to have the user to enter a letter whether it is u9932, 98t4. 9ngnmd or 23h345. I want the error to show and not crash the system. I don't know if the textbox text change might have something to do with it.
Computer Wiz99 13-Nov-14 14:38pm
Check out my error update.
PIEBALDconsult 13-Nov-14 14:46pm
Yes, that confirms my suspicion that the Expression is valid, but probably not what you intended.
Or, as Kornfeld said, the operations are happening out of order. Maybe you should combine the two validators into one?
Computer Wiz99 13-Nov-14 14:51pm
How do I combine the two validators into one? In my case I don't think I can do that. I have another code that handles the rangevalidators.

I'm just guessing that you probably want ^\d+$ .
It is not clear from your question where you exactly got the error, but one thing is sure - your regex not good...
[0-9] is good only for a single digit. If you want more than that user [0-9]+ or \d+ (d is for digit), and forgot the ^ (or $) signs - your input is single line so there is not much meaning for start/end-of-line here...[^]
About your RangeValidator...You have no range! Also there is a problem with that Type attribute - it will force the validator to convert the textbox value to integer before comparsion...
It is unsure what will be the order of the validations in case of multiple validators...So the range validation may run before the regex and in case you have something that can not be converted to int you will get the error above...
In your case only CustomValidator can help...
Computer Wiz99 13-Nov-14 15:00pm
How can I setup CV in my case. I have a code that runs the RangeValidator when the page loads.
Kornfeld Eliyahu Peter 13-Nov-14 15:25pm
I'm not sure what your case is...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900