SO Help: How to check value of a textbox that contain _age at the end?
http://stackoverflow.com/questions/16962379/how-to-check-value-of-a-textbox-that-contain-age-at-the-end/16962472#16962443
by SpYk3
HTML
<input id="bob" type="text">
<input id="pax_9495237e-5c9e-489f-8700-2f82e211fd51__Age" type="text">
<input id="bil" type="text">
<input id="pax_9495237e-9h7e-489f-8700-2f82e211fd51__Age" type="text" value="1">
<input id="bad" type="text">
<input id="pax_9495237e-9k2e-489f-8700-2f82e211fd51__Age" type="text" value="69">
CSS
input[type=text] { display: block; width: 90%; }
JavaScript
// an array of e.which|e.key codes that is every key code for every number and control (like shift, backspace, etc..)
var numsNcontrols = [48,49,50,51,52,53,54,55,56,57,96,97,98,99,100,101,102,103,104,105,8,9,13,16,17,18,19,20,32,33,34,35,36,45,46,44,145,37,38,39,40];
// first grab all inputs ending with "__Age", then asign "keydown" check for improper characters
$("input[type=text]").filter(function(i) { return $(this).prop("id").indexOf("__Age") !== -1; }).on("keydown", function(e) {
if (e.ctrlKey && e.which == 65) return true; // if is ctrl+a (select all)
if ($.inArray(e.which, numsNcontrols) == -1) return false;
}) // now, using jQuery Chaining, we continue and asign keyup event to alert users they cannot enter anything but numbers
.on("keyup", function(e) {
if (e.ctrlKey && e.which == 65) return true; // if is ctrl+a (select all)
if ($.inArray(e.which, numsNcontrols) == -1) alert('Numbers only please!');
})
// simple check value update to show how many textboxes end in "__Age"
$("#bil").val(
$("input[type=text]").filter(function(i) { return $(this).prop("id").indexOf("__Age") !== -1; }).length
+ " textboxes have __Age at end of ID."
);
// Shows in a textbox how many textboxes end in "__Age" && have a numeric value
$("#bad").val(
$("input[type=text]").filter(function(i) { return $(this).prop("id").indexOf("__Age") !== -1 && !isNaN(parseInt($(this).val())); }).length
+ " textboxes have __Age at end of ID && numeric value."
);