-2

Any idea how to validate a phone number in javascript without using RegEx?

The phone number should be in this format:

xxx xxxxxxx

x -> number

Fale1994
  • 67
  • 14
  • Seems this had been answered [here](http://stackoverflow.com/questions/16699007/regular-expression-to-match-standard-10-digit-phone-number) – Hang Apr 15 '16 at 23:55
  • A series of checks: `x.length == 11` being one of them, `x[3] == ' '` being another... – bozdoz Apr 15 '16 at 23:55
  • yes it is possible to validate you're required pattern without using regex but let us see what you have tried first. – Shub Apr 15 '16 at 23:56
  • Doesn't look like a non-regex answer @Hang – bozdoz Apr 15 '16 at 23:56
  • Ah sorry about that, I missed the word `without`. If this is the only format you are going to accept, you can do a while loop and check character by character, to make sure all of them are digits other than the 4th one, which should be a space. – Hang Apr 15 '16 at 23:58
  • why do you want to avoid regex – chiliNUT Apr 16 '16 at 00:42

1 Answers1

3

Well, there are a few ways I'm sure you could go about doing this.

First thing that comes to mind would be to put the string into an array of fixed length and then iterate through the array. if the array at position [3] is anything but a space, you know it's not a valid formatted number. also, if the re are too few or too many items for the array, then you know its invalid. and lastly, when you iterate through each item of the array, and it's not a number (or a space at the 4th position [3], then you know it's not valid.

flux9998
  • 330
  • 2
  • 11