-1

I am using isNAN at the moment but I can't seem to get it to work.

if (isNAN(getElementById("Bob")))

4 Answers4

1

You can use something along the lines of this function to basically check if inp has letters in it.

function checkLetter(inp)  
{  
   var letters = /^[A-Za-z]+$/;  
   if(inp.value.match(letters)){  
      //Letters 
   }  
   else {  
      //no letter
   }  
}  
Bhushan
  • 6,151
  • 13
  • 58
  • 91
Tyler McGinnis
  • 34,836
  • 16
  • 72
  • 77
0

Try this:

if (isNaN(document.getElementById("Bob").value))

You are not using proper syntax in your code to get value. Also make sure you are using isNaN() and not isNAN().

If you want to check empty values also:

var myVar = document.getElementById("Bob").value;
if(!myVar || isNaN(myVar))
    alert("Not a number!");
else
    alert("Is a number!");
Bhushan
  • 6,151
  • 13
  • 58
  • 91
0

you have to check the value, not the object

if (isNAN(getElementById("Bob").val()) ){
 //
}

EDIT so if you just want lock the field contains numeric values you can do this in html5 :

<input type="number" />
Ema.H
  • 2,862
  • 3
  • 28
  • 41
0

Here is a way to check if only number exists not letters

function isNumber(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}

So the use will be like

nn=document.getElementById("Bob").value;

ans=isNumber(nn);

if(ans)
{
    //only numbers
}

This ans was found from here

Validate numbers in JavaScript - IsNumeric()

Community
  • 1
  • 1
AtanuCSE
  • 8,832
  • 14
  • 74
  • 112