0

There is lots of answers for that question on SO but none for javascript...

How can I check if first 16 characters of this sting 20130203003002od is a number? Meaning that the substring doesn't contains any letters nor any other characters but digits?

2
  • Somebody suggested isNaN(value) function. But this comment was removed any idea why? Commented Feb 11, 2013 at 22:43
  • because the asker is only interested in finding if the first 16 characters contain only digits. isNaN('0xf'), for example will return false (meaning it is a number) and that isn't what the asker wanted since x and f aren't digits. Commented Feb 13, 2013 at 15:36

2 Answers 2

6

if first 16 characters ... [don't contain] any letters nor any other characters but digits

/^\d{16}/.test(str);

should work.

Sign up to request clarification or add additional context in comments.

Comments

1

You could test the string against a regular expression like so:

var data = "20130203003002od";
var matched = data.match("[0-9]{16}.*");

If matched is NULL, that means that the first 16 characters are not a number.

1 Comment

I like your answer. Thank you. Works nicely.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.