-2

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";
}
0

1 Answer 1

0

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"> 

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.