detect if the variable contains letters or javascript numbers

advertisements

I've been looking on the internet but couldn't find a proper solution to my problem.

I have two use inputs. One is Age. The other is Name.

Age must be a number. Name must be a word, containing only characters.

if(age==number $$ name == characters){

    // do something

}else{

    // do something else

}

I tried:

if(Math.floor(num) == num $$ Math.floor(name) != name){

    // do something

}else{

    // do something else

}

But no luck whastsoever.

If you know how to do this could you let me know. Thanks a lot.


The regex solution is probably the simplest given that your input will be strings anyway.

Try this:

if (/^\d+$/.test(age) && /^\D+$/.test(name)) {...}

The main difference with the ones posted above is in the ^ and $; these will make sure that the string containts only the specified character, as opposed to them containing at least one.