Solved

RangeValidator accepts empty textbox

Posted on 2009-07-01
4
790 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 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