Solved

RangeValidator accepts empty textbox

Posted on 2009-07-01
4
792 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

707 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