Solved

RangeValidator accepts empty textbox

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

896 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

11 Experts available now in Live!

Get 1:1 Help Now