Input Mask for Textbox

I have some Textboxes on a web page.  Users are supposed to only put a numeric value between 0 and 100 in them.  How can I specify an Input Mask or Validation Rule that will prevent non-numeric characters or negative values or values greater than 100 from being entered?
LVL 1
wsturdevAsked:
Who is Participating?
 
silemoneConnect With a Mentor Commented:
skeleton of the validator:
<asp:RegularExpressionValidator
     id="ProgrammaticID"
     ControlToValidate="ProgrammaticID of control to validate"
     ValidationExpression="expression"
     ErrorMessage="Message to display in ValidationSummary control"
     Text="Message to display in control"
     ForeColor="value"
     BackColor="value"  
     runat="server" >
</asp: RegularExpressionValidator>

example:

<asp:RegularExpressionValidator
     id="ProgrammaticID"
     ControlToValidate="txtAge"
     ValidationExpression="^(100 | [0-9] | [1-9][0-9] )$"
     ErrorMessage="Age range is 1 - 99"
     ForeColor="value"
     BackColor="value"  
     runat="server" >
</asp: RegularExpressionValidator>


0
 
silemoneCommented:
use regular expression validator and allow only up to 3 digits that only

i.e.

(100 | [0-9] | [1-9][0-9] )
0
 
silemoneCommented:
that allows 100 or 0-9 (single digits) or 10 - 99
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
wsturdevAuthor Commented:
Okay.  I also want to use it for a textbox that is part of a GridView.  How do I do that?
0
 
wsturdevAuthor Commented:
I figured out that it goes in the ItemTemplate.
The functionality was not as expected, though.  The height of the Grid row is increased in case an invalid value is entered and it is necessary to show the error message.  Is there any way to produce an error dialog instead?
0
 
wsturdevAuthor Commented:
After playing with it for a while, I kind of like the error coming up as it does.  However, it would be nice if the Grid row would be sized "normally" until an error needs to be displayed.  Is that possible?  (e.g. normal grid row height is 50px, and it displays that way, but when an error is displayed, the row increases to 150px.)
0
 
samsymonCommented:
use scripts for that.
Textbox1.Attributes.Add("onKeyDown", "return functionname(event,this,'seprator');")

you can write the jscript in functionname

if the function return false u cannot enter the other than u specified.





0
 
samsymonCommented:
if u want to use it in grid then specify like this

onkeydown="return functionname(event,this);"
0
 
wsturdevAuthor Commented:
Actually, by putting it inside the ItemTemplate as in the attached code, as soon as you tab out of the field, the validation is performed and the error message shows.
And, I found out if you specify Display="Dynamic", the row will remain normally sized until the error is found, at which time it will dynamically expand to accommodate the error message.
I cannot find anywhere that shows instructions on how to build a ValidationExpression.  As you can see by my code, I am displaying a value in a text box as a percentage.  I need the ValidationExpression to allow for percent signs (%).  Otherwise, any value I display with a percent sign will be considered an error.
1. Can you modify the ValidationExpression you gave me to allow for percent signs?
2. Can you direct me to a link that shows how to build ValidationExpressions for future use?

        <asp:TemplateField HeaderText="Day 5 Percentage" SortExpression="Pulse_Check_Day_5_Completion_Percent">
          <ItemTemplate>
            <asp:TextBox ID="Pulse_Check_Day_5_Completion_Percent" runat="server" Enabled="True"
              Font-Bold="False" Font-Names="arial" Font-Size="8pt" OnTextChanged="Item_Change"
              Style="text-align: center" Text='<%# Bind("Pulse_Check_Day_5_Completion_Percent","{0:0%}")%>'
              Width="55px"></asp:TextBox>
            <asp:RegularExpressionValidator 
               id="Pulse_Check_Day_5_Completion_Percent_Validator" 
               ControlToValidate="Pulse_Check_Day_5_Completion_Percent" 
               ValidationExpression="^(100 | [0-9] | [1-9][0-9] )$" 
               ErrorMessage="<b>Number only!</b><br>(0 to 100)"
               ForeColor="#ff0000"
               BackColor="Transparent" Display="dynamic" 
               runat="server" >
            </asp:RegularExpressionValidator>
         </ItemTemplate>
          <ControlStyle Width="55px" />
          <ItemStyle HorizontalAlign="Center" VerticalAlign="Top" Width="55px" />
        </asp:TemplateField>

Open in new window

0
 
wsturdevAuthor Commented:
I need a bit more...  Once the error is displayed, if I go back to the textbox and correct the value and then tab out of it, the error does not go away.  What am I not doing?
0
 
wsturdevAuthor Commented:
I seem to have a more basic problem.  I was originally keying in bad values so I could see the error message come up.  but now I realize when I even key in valid values, the error message comes up.
Is there a problem with the ValidationExpression as it is currently coded, given the data is bound?
0
 
wsturdevAuthor Commented:
I ended up using a CustomValidator with OnServerValidate set to the name of a VB handler that performed the validation.
I am going to close the question, but would still like to be pointed to a link showing how to properly code ValidationExpressions, if you can provide it.
0
 
wsturdevAuthor Commented:
I did use your suggestions in some places, and while I used a different concept for my GridView, you led me to it!  Many thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.