A very noob here. I've searched the forum and found lots of good posts but not one that exactly matches my problem so here it is.

All I want to do is check for symbols in a telephone number. I pick up the variable from the form and check it like this:

function VerifyForm(&$values, &$errors)
{
if(!preg_match("[0-9]",$values['telephone']))
$errors['telephone'] = 'no symbols';

if(empty($values['telephone']))
$errors['telephone'] = 'Please enter a telephone number';
}

The form field is this:
<p class="error"><?= $errors['telephone'] ?></p>
<p><input type="text" size="20" name="telephone" value="<?=htmlentities($values['telephone']) ?>"/></p>

If I put letters or symbols it returns the error. But it also tells me that 0-9 are symbols as well! It's going to be something really simple but I can't see it.

Note if you leave the field blank the correct error message displays.

The test form is here: http://www.aerin.co.uk/contactform/errors3.php
All the other fields work fine.

PS: This one works but just strips out the dodgy characters, I want to be a bit more user friendly: http://www.aerin.co.uk/contactform/errors.php

    I don't think you want to negate ("!") the preg_match(), as you want the if condition to be true if it finds anything that is not a 0-9, correct?

      That's correct, I hadn't realised the ! negated everything. If this is the case how come it negates the 0-9 and the letters and symbols?

        fisicx;10932892 wrote:

        That's correct, I hadn't realised the ! negated everything. If this is the case how come it negates the 0-9 and the letters and symbols?

        [0-9] matches any character except for 0 through 9 🙂

        /^[2-9]\d{2}-\d{3}-\d{4}$/

        If it helps, the above regex will match a hyphen separated US phone number.

          Thanks jaql,

          I think I'm going bonkers here. This should be such a simple check so I must be missing something.

          What I want to do is check the whole string for anything other than 0-9, the symbols +-() and a space. If there is an match then the error message asks the user to check their telephone number.

          I could easily strip out the offending characters but I'm trying to be a little more helpful.

          Suggestion please?

          PS: I'm not the best programmer in the world, creating forms is about my limit at the moment

            Try this:

            $pattern = '/((\(\d{3}\)?)|(\d{3}))([\s-+]?)(\d{3})([\s-]?)(\d{4})/';
            if(!preg_match($pattern,$number)) {
              // error code
            }
            else {
              // success
            }
            
              fisicx;10932960 wrote:

              Thanks jaql,
              What I want to do is check the whole string for anything other than 0-9, the symbols +-() and a space. If there is an match then the error message asks the user to check their telephone number.

              I could easily strip out the offending characters but I'm trying to be a little more helpful.

              Suggestion please?

              My suggestion would be to strip out offending characters, look at what numbers you have left (to see if the length of numbers matches the phone number format you desire (ie. if area code is present or not)).

              One serious pet peeve when dealing with online forms is when a site forces an user to format something specifically. I never understood why they did this. Take the 'No Dashes or Spaces Hall of shame' site (which belongs to the brother of Jeffrey Friedl, who is the author of Mastering regular expressions) for instance. That site maintains that making it a requirement of no dashes or spaces is akin to programmer laziness (and I agree with that completely).

              So back to phone numbers... why punish an user if they format it like (555) 555-5555 or 555-555-5555 or 555.555-5555, etc..? This is a sure fire way to frustrate legitimate, honest, well intentioned users.. let them format the number to their liking.. and simply look at only the numbers they leave and see if that is the length is valid or not, and base that info on whether or not the user has commited an error. Afterall, with all non-numerics stripped out, nothing stops you from reformatting it for them.

              Example (based on north american number with area code):

              $input = '555.246.5555'; // not the format we want, but that's ok...
              $phoneNumber = preg_replace('#[^0-9]#', '', $input);
              if(strlen($phoneNumber) == 10){
                  // valid north american formatted phone number (area code included)
                  // Now we can reformat this to our liking instead of being too strict with the end user
                  echo $phoneNumber = '(' . substr($phoneNumber, 0, 3) . ') ' . substr($phoneNumber, 3, 3) . '-' . substr($phoneNumber, -4); // Output: (555) 246-5555
              } else {
                  // warning: improper amount of numbers used. Ask user to re-verify
              }
              

              There are far too many restrictive forms out there that don't make life easier for the user (all because the programmer wants things in a specific format, yet is not willing to do that part for the user, and instead burdens the user (which I might add risks the user abandoning the site in question and going elsewhere.. after all, who likes hassles)?

                4 days later

                Thanks eveyone for their help.

                It's all sorted now due to my complete ignorance on how regular expressions work. however with your suggestions and lots of reading I got it to do just what I want!

                It now checks for an empty string and checks for the dodgy charaters in the string. I've set it to allow 0-9 -+() and spaces. It doens't check for formatting because as suggested tyhis can have so many variants.

                So you will get one of two error messages: 'Please enter a telephone number' or 'Please check your telephone number' which I think covers everything.

                  Write a Reply...