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 test if a variable has numbers javascript

I would like to know how I could test if a variable has numbers
I tried with two codes but none worked.

var adressevalidation = document.getElementById("adresse").value;
console.log(adressevalidation);

//test de l'adresse il doit y avoir un numero valable pour tous les pays
if (!preg_match('#[\d ]#', (adressevalidation))) {
  document.getElementById("myadresse").innerHTML = "pas de numero";
  document.getElementById("adresse").style.backgroundColor = "red";
}

if (!adressevalidation.match(/^([a-zA-Z ]+)$/)) {
  document.getElementById("myadresse").innerHTML = "pas de numero";
  document.getElementById("adresse").style.backgroundColor = "red";
}

>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

A simple function for that

var adressevalidation = document.getElementById("adresse").value;


function containsNumbers(str) {
  return /\d/.test(str);
}

console.log(containsNumbers(adressevalidation))
<input type="text" id="adresse" value="sfg5rgb6hd"> 
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