Javascript - validation, numbers only

前端 未结 14 1713
感动是毒
感动是毒 2020-12-08 09:52

I\'m trying to get my login form to only validate if only numbers were inputted. I can it to work if the input is only digits, but when i type any characters after a number,

14条回答
  •  误落风尘
    2020-12-08 10:24

    Late answer,but may be this will help someone

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

    Use will be like

    nn=document.forms["myForm"]["num"].value;
    
    ans=isNumber(nn);
    
    if(ans)
    {
        //only numbers
    }
    

    This ans was found from here with huge vote

    Validate numbers in JavaScript - IsNumeric()

提交回复
热议问题