Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 3 of 3
  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    11
    Member #
    36185
    Hello everyone!

    I'm new to this forum.

    Basically I am following a tutorial on youtube on how to add a contact form to a page. I have the least knowledge about PHP. The instructor's form has three inputs (Name Email Message) but i have decided to add another (Phone Number). The problem I have now is to get PHP to echo "Please enter a valid Phone Number" whenever there is an error. For example if the visitor inputs letters instead of numbers.

    Below is the code being used.

    PHP Code:
    <?php
    if (empty($_POST) === false) {
        
    $errors = array();
       
        
    $name        $_POST['name'];
        
    $email        $_POST['email'];
        
    $number    $_POST['number'];
        
    $message    $_POST['message'];
       
        if (empty(
    $name) === true || empty($email) === true || empty($number) === true || empty($message) === true) {
            
    $errors[] = 'Name, Email, Phone Number and Message are required';
        } else{
           
            if (
    filter_var($emailFILTER_VALIDATE_EMAIL) === false) {
                
    $errors[] = 'That\'s not a valid Email Address';   
            }
           
            if (
    ctype_alpha(str_replace(' '''$name)) === false) {
                
    $errors[] = 'Name must only contain letters';
            }
           
                if(
    preg_match("/^([1]-)?[0-9]{3}-[0-9]{3}-[0-9]{4}$/i", ($number) === false)) {
                
    $errors[] = 'Name must only contain letters';
            }
     
     
     
           
        }
     
    }
    The Instructor is using ul to echo the error arrays, the $name $email echos work but the $number doesn't. by the way i added the (preg_match) after browsing for a solution on google.

    Here is the code that's used to echo any errors using ul.
    PHP Code:
    <?php
            
    if (empty($errors)  === false) {
                echo 
    '<ul>';
                foreach(
    $errors as $error) {
                    echo 
    '<li>'$error'</li>';
                }
                echo 
    '</ul>';
           
            }
            
    ?>
    Thank you!

  2.  

  3. #2
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,716
    Member #
    5580
    Liked
    718 times
    There are other issues to this because phone numbers are entered in various formats ....
    1-651-555-1234
    (651)555-1234
    651-555-1234

    The best thing to do is use Google to answer your PHP questions. You'll find many examples of various methods to use. Go to Google and search for this:
    php validate phone number


  4. #3
    Junior Member
    Join Date
    May 2013
    Posts
    11
    Member #
    36185
    Thanks for your reply mlseim. I gave up on adding a phone number field as it was taking up much of my time. My only solution now is to set a side time to learn some php coding. Thanks for the advice once again.


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 10:56 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com