function ValidateShippedQuantity() {
var shippedQty = jQuery("#txtShippedQuantity").val();
shippedQty = shippedQty.toString();
for (i = 0; i < shippedQty.length; i++) {
var c = shippedQty.charAt(i);
if (isNaN(c)) //(!(/^\d+$/.test(shippedQty)))
{
alert("Only Numeric Values Allowed");
//x.focus();
return false;
}
}
return true;
}
What I want is to check the textbox that contains only numeric value. And the function above either isNaN or /^\d+$/.test() does not work since it always returns false whatever I enter such as "1" or "10". Even weird, the isNaN used to work for a while. And then it just did not work later no matter what I undid what I did.
The button which called the validation function, is within a Gridview.
<EditItemTemplate>
<asp:LinkButton ID="btnUpdTrk" runat="server" Text="Update" CommandName="Update"
OnClientClick="javascript:return ValidateShippedQuantity();" CausesValidation="false" />
</EditItemTemplate>
The textbox of txtShippedQuantity,
<asp:TemplateField HeaderText="Shipped Qty">
<ItemTemplate>
<asp:Label ID="lblShippedQuantity" runat="server" Text='<%#Eval("ShippedQuantity")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtShippedQuantity" Width="50px" Text='<%#Eval("ShippedQuantity")%>' />
</EditItemTemplate>
</asp:TemplateField>
For those who has the same problem, the answer or solution is below. This is the real happiness of solving the problem yourself after disappointing procedure. @cymen gives me a little help. And I change one line to his codes.
$(document).ready(function () {
$('#btnUpdTrk').on('click', ValidateShippedQuantity);
});
function ValidateShippedQuantity() {
var shippedQty = document.getElementById('ContentPlaceHolder1_gvTrkInfo_txtShippedQuantity_0').value;
var shippedQtyNumber = parseInt(shippedQty, 10);
if (shippedQtyNumber.toString() !== shippedQty) {
alert("Only Numeric Values Allowed for Tracking #.");
return false;
}
return true;
}
The second line from @cymen codes, is the cause of problem for my aspx page, at least after editing that I got what I wanted. I think it's the getTlementById part. After finding the correct ID for txtbox txtShippedQuantity from the google chrome developer tool.
return /^\d+$/.test(jQuery("#txtShippedQuantity").val())