Area Code Validation on Phone Number Field

  • 4 February 2019
  • 5 replies
  • 54 views

Hi everyone!

We have a need to validate the area code on the phone number field from a list of acceptable area codes. Does anyone have a script to check the first three digits against a list of approved three digit combinations and validate?

The need is coming from a number of Google Smart Display campaigns that are generating fake leads from countries outside our target country for the client and in Google. The placement owers of these ads are trying to send additional quality signals into the platform to capture more budget from automated campaigns using machine learning signals against Google.

Any help is appreciated!

Thanks
Kelly


5 replies

Userlevel 5

Hi!

Have you tried using the phone number validation that is already in the Unbounce builder?

I got this code that might help you (just edit the first 2 lines with the id of your country and phone field). The script also doesn’t let the user type any character except “+” and “0-9”, it also changes “00” to “+” when it’s typed at the beginning and it doesn’t accept spaces:

<script>
  var pais = document.getElementById("pais");
  var telefono = document.getElementById("telefono");
  
  function sustituyeTelefono() {
	if (telefono.value === '00') {
		telefono.value = '+'
	}
};

telefono.addEventListener('input', sustituyeTelefono);
  
function cambiaExtension() {  
  switch (pais.value) {
    case "Colombia":
      telefono.value = '+57';
      break;
    case "Mexico":
      telefono.value = '+52';
      break;
    case "Argentina":
      telefono.value = '+54';
      break;   	
    case "Chile":
      telefono.value = '+56';
      break;
    case "Peru":
      telefono.value = '+51';
      break;
    case "El Salvador":
      telefono.value = '+503';
      break;
    case "Honduras":
      telefono.value = '+504';
      break;
    case "Guatemala":
      telefono.value = '+502';
      break;
    default:
      telefono.value = '+';	

  }
};
  
pais.addEventListener('change', cambiaExtension);
  
function todoNumeros(){
      var invalidMatcher = /[^0-9+]+/;
      telefono.value = telefono.value.replace(invalidMatcher, '');
};
  
telefono.addEventListener('input', todoNumeros);

  
</script>

please can you explain that in a short video cause i try that but dosn’t work please

Hi, did you find some video?

I wrote that code in the javascript of the unbounce app butthere are no changes, if it should go there?

Reply