2
var tel = "123457890";
if (tel.length != 10) {
    console.log("Sorry, incorrect format.");
} else {
    var areaCode = tel.substring(0,3);
    var prefix = tel.substring(3,6);
    var suffix = tel.substring(6,10);
    console.log("(" + areaCode + ") " + prefix + "-" + suffix);
}

Everything about this works for me except I can't get it to check for digits only. When I troubleshoot it with suggestions and run it through http://repl.it/languages/JavaScript I run into error messages.

i.e. When I put a "w" in with a string of 10 numerical digits I want it to return "Sorry, incorrect format" as if I had put in the wrong amount of numbers, etc.

4 Answers4

5

You can use regex:

var tel = "123457890";
if (!tel.match(/^\d{10}$/)) {
    console.log("Sorry, incorrect format.");
} else {
    var areaCode = tel.substring(0,3);
    var prefix = tel.substring(3,6);
    var suffix = tel.substring(6,10);
    console.log("(" + areaCode + ") " + prefix + "-" + suffix);
}
Fabricator
  • 12,722
  • 2
  • 27
  • 40
1

Here's a fiddle:

http://jsfiddle.net/e1qq659g/

Here's a regex pattern:

http://regexr.com/3b77b

var regularExpression = /\d{10}/g;
(tel.match(regularExpression)) ? alert("All digits!") : alert("Not Digits!");

this will match the expression to the string in variable tel - if it does match it alerts "All Digits" - if it does not match it alerts "Not Digits!"

zfrisch
  • 8,474
  • 1
  • 22
  • 34
1

This works. It deletes all letters and checks if the length changes

var tel = "1234567890";
if (tel.length !== 10 || tel.length !== parseInt(tel).toString().length) {
    document.write("Sorry, incorrect format.");
} else {
    var areaCode = tel.substring(0,3);
    var prefix = tel.substring(3,6);
    var suffix = tel.substring(6,10);
    document.write("(" + areaCode + ") " + prefix + "-" + suffix);
}
James McDowell
  • 2,668
  • 1
  • 14
  • 27
1

You can use RegExp both to check if it only contains digits and to extract each part:

var tel = document.getElementById("tel");
var result = document.getElementById("result");

tel.oninput = function(e){
  //var match = tel.value.match(/^(\d{3})(\d{3})(\d{4})$/); // Basic RegExp
  var match = tel.value
    .replace(/\s/g, '') // Removing spaces before RegExp makes it simplier
    .match(/^(\d{3}|\(\d{3}\))(\d{3})\-?(\d{4})$/);
  
  if(match == null) {
    result.innerHTML = "Sorry, incorrect format.";
    return false;
  }
  
  // parseInt is optional
  var areaCode = parseInt(match[1].replace(/[\(\)]/g,'')); // Just need this with the second RegExp
  var prefix = parseInt(match[2]);
  var subfix = parseInt(match[3]); 
  
  result.innerHTML = "Valida input: (" + areaCode + ") " + prefix + "-" + subfix + "\n"
    + "\n\tArea Code: " + areaCode
    + "\n\tPrefix: " + prefix
    + "\n\tSubfix: " + subfix
};
<input type="text" id="tel" placeholder="Enter a valid phone number"/>

<pre id="result"></pre>

This way you could change the RegExp to make that match a formatted input too, lets say (123)456-7890, (123) 456-7890, (123) 456 - 7890, (123)4567890... that's what the current code does.

If you comment the current RegExp and uncomment the first one it will work just as you was asking for, accepting only a 10 digit input.

Danziger
  • 19,628
  • 4
  • 53
  • 83