1

So I would like to know what's inside the string for example:

var str = "a"; // Letter
var str = "1"; // Number
var str = "["; // Special
var str = "@"; // Special
var str = "+"; // Special

Is there any pre defined javascript function for this? Otherwise I will make it with regex :)

Adam Halasz
  • 57,421
  • 66
  • 149
  • 213
  • You might want to check this question: http://stackoverflow.com/questions/18082/validate-numbers-in-javascript-isnumeric before delving into Regular Expressions for numeric validation – Dancrumb Dec 11 '10 at 18:29
  • I don't think CIRK is wanting to do actual numeric validation, rather just determine the type of a single character. Certainly, that's the impression I get from the examples in their post. – El Ronnoco Dec 11 '10 at 19:06

3 Answers3

3
if (/^[a-zA-Z]$/.test(str)){
    // letter
} else if (/^[0-9]$/.test(str)){
    // number
} else {
    // other
};

Of course this only matches one character so 'AA' would end up in the //other section.

El Ronnoco
  • 11,753
  • 5
  • 38
  • 65
2

They are all strings...

There isn't anything built in that will do what you want.

A regex may be a good solution, though you have not really provided enough information for one.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • @Joel Coehoorn - Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems. ;) – Oded Dec 11 '10 at 18:27
0
if(isNaN(string)){
   //yes is a string
}
Conner
  • 30,144
  • 8
  • 52
  • 73
mavirroco
  • 117
  • 10