Phone number validation can be a bit tricky, especially considering the coutnless separation schemes people use to split area-code/exchange/number. My personal favorite was from a graphic designer I knew: "216/555^1234" Boy, if that's not a creative permutation of the limited symbols avaiable, I don't know what is. Anyway, to get around these, a lot of developers will just split the phone number into three separate input boxes. Peter noticed that his company did that on their contact form, too ...

<input type="text" size="3" maxlength="3" name="phone1" id="phone1" />
<input type="text" size="3" maxlength="3" name="phone2" id="phone2" />
<input type="text" size="4" maxlength="4" name="phone3" id="phone3" />

Fair enough. Of course, the (PHP) code behind it just doesn't seem to get it ...

if (!ereg("(^[[:digit:]]{3})\.([[:digit:]]{3})\.([[:digit:]]{4})$", "$phone1.$phone2.$phone3")) {
  $error[] = "invalid_phone";
} else {
  $phone = $phone1.$phone2.$phone3;

Peter explains ... This is a slightly bizarre way to go about it, since one would naturally just check each segment of the phone number to make sure it's valid, and then add them at the end. But what's really funny is the readability of this code. See, the regular expression is looking for something of the form "123.456.7890", because in that line the dot characters are used as strings. But in the assignment line, the dot characters are used as PHP's concatenation operator, so in this example the $phone variable would equal "1234567890".

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!