0

I am checking if a input field is empty but would like to check if it is numeric since it is a street address. I only want numeric to be allowed, no alpha even though an address might be like 123a or something else.

I want to keep the same format cause I'm checking other fields in the same function and want it to be the same. Below is what I have for empty field. What is the easiest way to change it to check for numeric only. I was trying if( !isNumeric (numbr.value)) but it isn't working.

var numbr = document.getElementById( "numbr" );
if( numbr.value == "")  {
    error = "Address number must be entered. Numeric only.";
    document.getElementById( "error_para" ).innerHTML = error;
    return false;
    }
bre
  • 27
  • 5

2 Answers2

0

You could also use parseInt and check if it is NotANumber:

window.onload=function(){

    document.getElementById("numbr").addEventListener("input",function(){
    document.getElementById( "error_para" ).innerHTML = (isNaN(parseInt(this.value,10)))?"Address number must be entered. Numeric only.":"";
    });
    
  };
<input id="numbr">
<div id="error_para"></div>
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
0

I used isNaN and it worked.

var numbr = document.getElementById( "numbr" );
if(isNaN(numbr.value))  {
    error = "Address number must numeric only.";
    document.getElementById( "error_para" ).innerHTML = error;
    return false;
    }
bre
  • 27
  • 5