Solved

RangeValidator accepts empty textbox

Posted on 2009-07-01
4
784 Views
Last Modified: 2012-05-07
I have a textbox that requires the user enter a number into.
In order to make sure they enter something and it is a valid integer I thought a rangevalidator would do the trick where the range is 0 -99999
If they try to enter text or 0.5 then everything works fine but if they have an empty text box or spaces then the form is submitted. Did I miss something?

<asp:RangeValidator ID="RangeValidator1" runat="server"
                        ControlToValidate="txtHoursLost" ErrorMessage="*Invalid value"
                        ForeColor="White" MaximumValue="99999" MinimumValue="0" Type="Integer"></asp:RangeValidator>
0
Comment
Question by:QPR
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
funwithdotnet earned 500 total points
ID: 24759511
It accepts an empty box because it is not required. Use a RequiredValidator with the RangeValidator.
0
 
LVL 29

Author Comment

by:QPR
ID: 24759720
hmmm so 2 validators tied to the same control. ok done that.
Seems to work ok until the person 9successfully) enters:
 "               7" for example.
Should the range validator not recognise that this is not an integer?
Yes I could start trimming stuff and doing further validations in the code behind but....?
0
 
LVL 12

Expert Comment

by:funwithdotnet
ID: 24759920
I believe the behavior is because all TextBox.Text properties are strings and must be converted to an integer before validating the range. The Interger.TryParse method will return True when supplied with the string "      7", therefore that string represents the integer 7.

I looked it up:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.rangevalidator.aspx
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.basecomparevalidator.type.aspx
0
 
LVL 15

Expert Comment

by:jinal
ID: 24760429
TRy to look for customvalidator or regualrexpression validator. It will give you more control over validation.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now