I am using isNAN at the moment but I can't seem to get it to work.
if (isNAN(getElementById("Bob")))
I am using isNAN at the moment but I can't seem to get it to work.
if (isNAN(getElementById("Bob")))
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
}
}
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!");
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" />
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