Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a textbox :

<asp:Label ID="lbltxtcitycode" runat="server" CssClass="col-md-4 control-label font-isstyle-large fontisstylealign" Text="<%$ Resources:EN_Resource, BIS00013 %>">


<asp:TextBox ID="txtCityAbbr" runat="server" CssClass="form-control">
<asp:RequiredFieldValidator ID="rfvCityAbbr" runat="server" Display="None" ControlToValidate="txtCityAbbr"
ErrorMessage="City abbreviation missing." ToolTip="Enter city abbreviation.">
<asp:ValidatorCalloutExtender ID="vceCityAbbr" CssClass="errorstyle" runat="server" TargetControlID="rfvCityAbbr" PopupPosition="BottomLeft"
Enabled="true" CloseImageUrl="~/_ImgIcons/clo.png" WarningIconImageUrl="~/_ImgIcons/war.png">

For allowing only numbers wrote script as :
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">



$(document).ready(function () {
$("#txtCityAbbr").keydown(function (e) {

if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||

(e.keyCode === 65 && (e.ctrlKey === true || e.metaKey === true)) ||

(e.keyCode >= 35 && e.keyCode &lt;= 40)) {

return;
}
// Ensure that it is a number and stop the keypress
if ((e.shiftKey || (e.keyCode &lt; 48 || e.keyCode > 57)) && (e.keyCode &lt; 96 || e.keyCode > 105)) {
e.preventDefault();
}
});
});

<asp:UpdatePanel runat="server">
<contenttemplate>

.
.
.

What I have tried:

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">



$(document).ready(function () {
$("#txtCityAbbr").keydown(function (e) {

if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||

(e.keyCode === 65 && (e.ctrlKey === true || e.metaKey === true)) ||

(e.keyCode >= 35 && e.keyCode &lt;= 40)) {

return;
}
// Ensure that it is a number and stop the keypress
if ((e.shiftKey || (e.keyCode &lt; 48 || e.keyCode > 57)) && (e.keyCode &lt; 96 || e.keyCode > 105)) {
e.preventDefault();
}
});
});

<asp:UpdatePanel runat="server">
<contenttemplate>
Posted
Updated 5-Jun-17 22:21pm

1 solution

javascript runs off what is sent to the client

$("#txtCityAbbr").


View the source of your page, do you see any elements with an id of txtCityAbbr? asp.net can change the IDs of your components, if the js is on the aspx page itself then use ClientID

$("#<%=txtCityAbbr.ClientID%>").
 
Share this answer
 
Comments
Member 13174150 7-Jun-17 1:37am    
not working
F-ES Sitecore 7-Jun-17 4:09am    
You're going to have to improve your debugging skills then because "not working" doesn't give anyone enough information to help.

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