0

This might be some odd question but it is the problem I am facing. I have textBox with Id like:

"pax_9495237e-5c9e-489f-8700-2f82e211fd51__Age"
"pax_9495237e-9h7e-489f-8700-2f82e211fd51__Age"
"pax_9495237e-9k2e-489f-8700-2f82e211fd51__Age"

Now I want to check if all Textboxes consist of __Age at the end, has numeric value or not. If not numeric(INT) i.e. characters(no .) not allowed then make an alert. Please help me I don't know how to do it. I know I have a class option but I want to do by Id.

Dhwani
  • 7,484
  • 17
  • 78
  • 139

5 Answers5

0

Try like this

$(document).ready(function(){
    $("input[type='text'][id$='__Age']").each(function(){
        if(isNaN(parseInt($(this).val())))
            alert('Not an Integer');                
        else
            alert('It is an Integer');
    });
});

It will check all the textbox values that are ids ended with '__Age'

GautamD31
  • 28,552
  • 10
  • 64
  • 85
  • If i am not misstaken, .val() will return a string. You need to parse the string before `isNaN` else it will always return false. – Karl-André Gagnon Jun 06 '13 at 12:54
  • Well. I am adding it dynamically, on button click. That is multiple age fields by clicking on add button. So I put on document.ready but it is not working. – Dhwani Jun 06 '13 at 12:59
  • Actually, i've tested my statement and it is actually false, you don't need to parse. But since the OP don't want decimal value, you need to parseInt – Karl-André Gagnon Jun 06 '13 at 12:59
  • @Gautam3164 The condition of `if` or the alerts is wrong, change the second alert for the first and the first for the second... xD – MG_Bautista Jun 06 '13 at 13:25
0

Something like:

$("textarea").filter(function(i) { return $(this).prop("id").indexOf("__Age") !== -1; })

http://api.jquery.com/filter/
http://api.jquery.com/jQuery.inArray/
How to check whether a string contains a substring in JavaScript?

Working Example

//    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 ($.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 ($.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."
);

¡ALSO! Find a pretty full listing in one big object (full of smaller objects and arrays) of Key Codes here

Community
  • 1
  • 1
SpYk3HH
  • 22,272
  • 11
  • 70
  • 81
0

To select you input, you can use this :

$('[id$="__Age"]')

Then to know if is a number

$('[id$="__Age"]').each(function(){
    if(isNaN(parseInt($(this).val()))){
        alert(this,id + ' is not a number');
    }
})

Fiddle : http://jsfiddle.net/3B9Vp/

Karl-André Gagnon
  • 33,662
  • 5
  • 50
  • 75
0

In order to get all input ending with "__Age" you can use the jquery selector

jQuery( "[attribute$='value']" )

You can find more details here

So, in your occasion you can call:

$('input[id$="__Age"]'); 

where all text boxes with an id ending in "__Age" will be returned.

In order to validate if the input is numeric you can check the value of each textbox and use isNaN function provided in Javascript

Here is an example where there are 3 text boxes and you call a function to check if their current value is numeric or not.

ppolyzos
  • 6,791
  • 6
  • 31
  • 40
0

If you only need to show one alert when any of the inputs with __Age at the end has nonNumeric values then you can try something like this:

if($("input[type='text'][id$='__Age']").filter(function(){
    return !isNaN($(this).val()); //leave only those inputs with nonNumeric value
}).length) //count their lenght - if it's > 0 then show alert
    alert('There are inputs with "nonNumeric" values!');
MythThrazz
  • 1,629
  • 17
  • 25