0

Possible Duplicate:
Validate numbers in JavaScript - IsNumeric()

I have the following (working) code that searches for the string "type 1" in my Json, but how can I search for a number? I am guessing that I need to change RegExp to Number, but I can't get it to work, it tells me that v.properties.code in not a function.

$.each(geojson.features, function (i, v) {
    if (v.properties.code.search(new RegExp(/type 1/i)) != -1) {
         Count++;
    }
});
Community
  • 1
  • 1
Bwyss
  • 1,736
  • 3
  • 25
  • 48
  • What was the RegExp that didn't work? Usually `\d` should be a (single) number. – m90 Dec 28 '12 at 08:29
  • Side note: if you already have a regular expression (`/type 1/i`) calling the `RexExp` constructor to create a regular expression is just redundant. – Álvaro González Dec 28 '12 at 08:41

1 Answers1

2

Numbers doesn't have the search function in their prototypes, so you just need to convert it to a string, this way you ensure it will always be a string, and you won't get that error, even though you should be doing a proper check on your content

$.each(geojson.features, function (i, v) {
  if (v.properties.code.toString().search(/type 1/i) !== -1) {
     Count++;
  }
});

or the other (less typing) way

$.each(geojson.features, function (i, v) {
  if (/type 1/i.test(v.properties.code)) { // does the conversion automatically
     Count++;
  }
});
pocesar
  • 6,860
  • 6
  • 56
  • 88