Monday, August 27, 2012

JavaScript looping through an array to check for numeric values

I have an an array that returns the values such as the following:



//Eg 1 [ [ \"214323\", \"34243\" ], [ \"3333\", \"123\" ] ]

//Eg 2 [ [ \"214323\" ],[ \"3333\" ] ]


I want to validate if the array holds only numbers with no spaces or null, else I would like to throw an error.

This is my following code and it does not work in the above example. It throws out an error even though all values are numeric.



for (var i = 0; i <= arrayVals.length; i++) {
if(!(/^\\d+$/.test(arrayVals[i]))) {
err_comp=true;

}
}

if( err_comp ==true) {
alert( \'The value has to be only numeric.\');
}






Rating: 1 out of 5 based on 3 ratings



The post JavaScript looping through an array to check for numeric values appeared first on Javascript ASK.






via Javascript ASK http://javascriptask.phpfogapp.com/javascript-looping-through-an-array-to-check-for-numeric-values.html

No comments:

Post a Comment