Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to check for a number in a textbox using javascript HTML?

I need to check if the value input in a HTML textbox contains a number, this is what I’m using so far, but it’s not working, can anyone help? The text box would be a mix of letters and numbers, but I want to check if there are any numbers at all.

<input id="input" type="text">
<button onclick="myFunction()">Submit</button>
<p id="HasNumber"></p>

<script>
function myFunction() {
if (document.getElementById("input").value >= '0' && value <= '9' {
HasNumber.innerText = "Has Numbers" ; }
else {
HasNumber.innerText = "No Numbers" ; }
}
</script>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can check if input contain number by using Regex like Below Example:

<input id="input" type="text">
<button onclick="myFunction()">Submit</button>
<p id="HasNumber"></p>

<script>
  function myFunction() {
    const inputVal = document.getElementById("input").value;

    let matchPattern = inputVal.match(/\d+/g);

    if (matchPattern != null) {
      HasNumber.innerText = "Has Numbers" ; 
    } else {
      HasNumber.innerText = "No Numbers" ; 
    }
  }
</script>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading