Javascript: Validate a field: Not empty & only contains numbers

Possible Duplicate:
Validate numbers in JavaScript - IsNumeric()

Hi I am trying to validate a field using javascript to ensure that the field is not empty AND only contains numbers

I am using document.getElementById to get the value of the field, then using this function to validate it:

function isNumeric(elem, message, messsage2)
{
   if(isNaN(elem))
   { 
    alert(message2); 
    return false; 
   }
   else if (elem == null || elem =="")
   {
    alert(message);
    return false;
   }
return true;
}

However this function still allows me to enter letters as where there should be numbers


function isNonEmptyNumber (str) {
  if(!str.length) return false;
  var num = +str;
  return isFinite(num) && !isNaN(num);
}

用正则表达式:

value.match(/^d+$/)
链接地址: http://www.djcxy.com/p/26400.html

上一篇: 如何在JavaScript中编写isNumber()?

下一篇: Javascript:验证一个字段:不为空&只包含数字