Click here to Skip to main content
15,904,297 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
--Scenario

--Now the below code is working fine. But there are two textbox in the page. But i dont want to write the below code for the other(2nd) textbox. Is it possible to add the othertextbox(2nd) id here in the below code. ?


XML
<script type="text/javascript">
   $(function() {
   $('[id*=txtdistanceops]').keydown(function(e) {
           if (e.shiftKey || e.ctrlKey || e.altKey) {
               e.preventDefault();
           } else {
               var key = e.keyCode;
               if (!((key == 8) || (key == 46) || (key >= 35 && key <= 40) || (key >= 48 && key <= 57) || (key >= 96 && key <= 105)))
               {
                   e.preventDefault();
                   alert("Please enter numeric value!");
                   $('[id*=txtdistanceops]').val('');

               }
           }
       });
   });
   </script>

   <script type="text/javascript">




--aspx page

XML
<asp:TemplateField HeaderText="Approved Km By Ops">
                    <ItemTemplate>
                        <asp:TextBox ID="txtdistanceops" MaxLength="6" CssClass="textBox_for6tds" runat="server"
                            Text='<%#Bind("distanceByOps") %>'>  </asp:TextBox>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="center" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Approved Km By CTV" Visible="false">
                    <ItemTemplate>
                        <asp:TextBox ID="txtdistancectv" MaxLength="6" CssClass="textBox_for6tds" runat="server">  </asp:TextBox>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="center" />
                </asp:TemplateField>



Please guide me.
Posted

1 solution

You will find some good options here:
jQuery: what is the best way to restrict “number”-only input for textboxes?[^]
EDIT:
Code Example
I do some code like you applied there and find it is working good for 2 textboxes that accepts only 6 digit numbers, hope it would be helpful for you. See:

JavaScript
<script type="text/javascript">
    function isNumberKey(evt) {
        var charCode = (evt.which) ? evt.which : event.keyCode;
        if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57)) {
            return false;
        } else {
            return true;
        }
    }
</script>

ASP
<asp:TextBox ID="TextBox1" runat="server" MaxLength="6" onkeypress="return isNumberKey(event)"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" MaxLength="6" onkeypress="return isNumberKey(event)"></asp:TextBox>
 
Share this answer
 
v5
Comments
anurag19289 22-Sep-13 8:50am    
I tried like this : Added class to both the textbox (test)

<asp:TemplateField HeaderText="Approved Km By Ops">
<itemtemplate>
<asp:TextBox ID="txtdistanceops" MaxLength="6" class="test" CssClass="textBox_for6tds" runat="server"
Text='<%#Bind("distanceByOps") %>'>

<itemstyle horizontalalign="center">

<asp:TemplateField HeaderText="Approved Km By CTV" Visible="false">
<itemtemplate>
<asp:TextBox ID="txtdistancectv" MaxLength="6" class="test" CssClass="textBox_for6tds" runat="server">

<itemstyle horizontalalign="center">


---then i tried in this way....not working
<script type="text/javascript">
$(function() {
$('.test').keydown(function(e) {
if (e.shiftKey || e.ctrlKey || e.altKey) {
e.preventDefault();
} else {
var key = e.keyCode;
if (!((key == 8) || (key == 46) || (key >= 35 && key <= 40) || (key >= 48 && key <= 57) || (key >= 96 && key <= 105)))
{
e.preventDefault();
alert("Please enter numeric value!");
$('[id*=txtdistanceops]').val('');

}
}
});
});
ridoy 22-Sep-13 14:08pm    
Check my edited answer and find whether it is helpful for you.
anurag19289 24-Sep-13 14:39pm    
i will try that and get back to you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900