views:

134

answers:

7

whats the best way to tell if a value in javascript is a single digit. Ive been doing something like

var valAsString = '' + val;
if (valAsString.match(/\d/) {}

clarification: I mean one of 0,1,2,3,4,5,6,7,8,9

Also, should what I have work? Im surprised how many different ways people are coming up with for this.

A: 

If you don't want to include negatives this will work, as will your solution.

var valAsString = val.toString();
if (valAsString.length === 1) {}

You could just check if the string is between -10 and 10 (assuming you want to include negatives). This will be fastest, but will not work for non-integers, so its probably best avoided.

If you do want to include negatives I'd probably check to see if the number is an integer then I'd go with something like this:

var isSingleDigitIncludingNegatives = function( arg ) {
    return ((typeof(myNum)=='number') && (myNum.toString().indexOf('.')==-1)) && (( -10 < arg ) && ( arg < 10))
CrazyJugglerDrummer
Type conversion can make things complicated, e.g.: `isSingleDigit("\t\r\n ") === true;`
CMS
4.5 is also a single digit.
Mark Byers
fixed stupid problems that I left in. Thanks guys :D
CrazyJugglerDrummer
A: 

Assuming that val is already numeric...

if ((val >= 0) && (val < 10) && (Math.floor(val) == val)) {
    // ...
}
LukeH
`val = "\t"` (ASCII 9) will cause it to identify as a number.
Andrew Dunn
@Andrew: That's why I said *"assuming that `val` is already numeric"*. If `val` isn't a number then the tests would need to change. The OP wasn't clear on whether or not `val` was numeric.
LukeH
A: 

How about something like this:

var check = parseFloat(val);
var isSingleDigit = (!isNaN(check) && (check < 10 && check > -10) && check.toString().length ==1);
Jage
So is `1.23456` a single-digit number?
LukeH
oh you are correct. Thank you. Edited based on your suggestion.
Jage
+1  A: 

Ummm, check if it's string length is equal to one?

if (typeof(val) === "number")
    {
    var valAsString = val.toString(10);
    if (valAsString.length === 1) {}
    }

This won't accept negative numbers or numbers with decimal components though.

Andrew Dunn
will this accept 'a'?
hvgotcodes
`typeof('a') !== "number"`
Andrew Dunn
Testing page: http://jsfiddle.net/LM8NR/
Andrew Dunn
good answer ...:-)
sushil bharwani
ahh didnt see the type test
hvgotcodes
If you look at the revisions you can see my previous code which was only 2 lines long if you were looking for less code. It did the same thing, it just incorporated the type check into the other `if` statement.
Andrew Dunn
+1  A: 

The /\d/ regexp will match a digit anywhere on a string, for example in "foo1" will match "1".

For a regexp approach need something like this, to ensure that the string will contain a single digit:

if (/^\d$/.test(val))  {
  //..
}

Note that I'm using the test method, which is recommended when you only want to check if a string matches the pattern, also, the test method internally will convert to sting the argument.

Another short non-regexp approach:

function isDigit(val) {
  return String(+val).charAt(0) == val;
}
CMS
@cms what is the + in +val doing?
hvgotcodes
@hvgotcodes, is doing type conversion to Number. It is basically ensuring that the `String` constructor gets a `Number` value as the argument. For example `typeof +'1' == 'number';`. `+` in this example, is the [unary plus operator](http://ecma262-5.com/ELS5_HTML.htm#Section_11.4.6)
CMS
checkmark because its the least amount of typing.
hvgotcodes
A: 

You can use the below modification of your regular expression:

valAsString.match(/^\d$/)

A: 

I think

(+val + val % 1 + 10) % 10 === val

should do it, assuming you only want to accept values of type number.

A cleaner solution is

typeof val === 'number' && val >>> 0 === val % 10

which can be easily adapted to include values of other types.

Christoph