0

I am new at Javascript and I wanted to know if there is a way to check if varibale in java scripts contains anything other than numbers. I have map variable where in I have JSON values as shown below. 'id' is the key of the map.

var map = {};
data = {"id":"01L","rowId":"01L","start":1399919400000,-- - -- -- ------}
map[data.id] = data;

'map[id].start' can have two kind of values as shown. In case 1, there will be only numbers and in case 2, there will be both nums and alphabets.I need to check for this two.

  1. 1399660200000
  2. Sat May 10 2014 00:00:00 GMT+0530.

So I need to check if 'map[id].start' contains any alphabets. I have tried the following three ways..But no luck.

method1

if((map[id].start).match(/[^0-9]/))  {
                 console.log("only numbers");
             } else {
                 console.log("alphabets also");
}
TypeError: match is not a function

method2

if(isNaN(map[id].start)) {
                 console.log("only numbers"); 
}else {
       console.log("not only numbers");
 }

method3

   if((map[id].start).indexOf(":")) > 0) {
             console.log("not only numbers");
    } else {
             console.log("only numbers"); 
    }

TypeError: s.indexOf is not a function

Please help me.

0

4 Answers 4

1
stringX = "Some string"
regExp=/^[\d]*$/; 
if(!regExp.test(stringX)) {
    //Do something
}

1399660200000 would trigger false as its only numbers

Sat May 10 2014 00:00:00 GMT+0530. would trigger true as its contains non numberic characters

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

Comments

0

You could edit Method 3 like this:

if(String(map[id].start).indexOf(":")) > 0) {
         console.log("not only numbers");
} else {
         console.log("only numbers"); 
}

String() will change the numbers in a string, so you could use .indexOf()

Comments

0

Method2 is the closest to being correct, all you need to do is either re-order the conditional, or negate it.

if(isNaN(map[id].start)) {
    console.log("not only numbers"); 
} else {
    console.log("only numbers");
}

Method1 fails because "foo".match() always returns an array and arrays are always truethy.

Method3 fails because numbers don't have an indexOf method.

2 Comments

Thanks Kevin..I have corrected my codes as you mentioned in method2. But it returns "only numbers" in both cases.
then i suspect map[id].start doesn't contain what you think it does. jsfiddle.net/9XaQF
0

To check if a javascript string contains only numbers:

var myStringWithOnlyNumbers = '1234567890';
var myStringWithOtherStuff = '1234567890.';

/^[0-9]+$/.test(myStringWithOnlyNumbers);
/^[0-9]+$/.test(myStringWithOtherStuff);

Working example

1 Comment

Thank you all for your timely help..I have resolved my issue using String() in method3. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.