简体   繁体   中英

Validate phone number in contact form

I have this form Link The validation should validate if a valid phone number is entered and if not then give the error text "Bitte geben Sie eine gültige Telefonnummer an", but the validation gives always the same message "Bitte geben Sie Ihre Telefonnummer an" in english please type in your phone number even if you enter invalid characters.

PHP:

function setParams($post)
{
    if ( ! $this->name = strip_tags(trim($post['name'])))
    {
        $this->error[] = 'Bitte geben Sie Ihren Namen an.';
    }
    if ( ! $this->phone = strip_tags(trim($post['phone'])))
    {
        $this->error[] = 'Bitte geben Sie Ihre Telefonnummer an.';
    } else if ( preg_match('#[^0-9\+\-\040/]#', $phone) )
    {
        $this->error[] = 'Bitte geben Sie eine gültige Telefonnummer an.';
    }   

    if ( ! $this->email = strip_tags(trim($post['email'])))
    {
        $this->error[] = 'Bitte geben Sie Ihre E-Mail-Adresse an.';
    } else if ( ! filter_var($this->email, FILTER_VALIDATE_EMAIL) || strstr($this->email, '@') === FALSE)
    {
        $this->error[] = 'Bitte geben Sie eine gültige E-Mail-Adresse an.';
    }
    if ( ! $this->body = strip_tags(trim($post['message'])))
    {
        $this->error[] = 'Bitte geben Sie einen Nachrichtentext ein.';
    }

HTML:

<form action="kontakt.php" method="post">
            <label for="name">Ihr Name:</label><input class="get1" type="text"     name="name" value="" /><br />
            <label for="telefon">Telefonnummer (bitte nur Ziffern):</label><input     class="get1" type="text" name="phone" value="" /><br />
            <label for="email">E-Mail-Adresse:</label><input class="get1" type="text"     name="email" value="" /><br />
            <label for="message">Ihre Mitteilung an uns:</label><textarea cols="20"     rows="5" name="message"></textarea><br />
            <label>Welche Leistungen interessieren Sie besonders?    </label>
            <label style="display:inline;" for="dachstuehle">Dachstühle</label>  <input     class="kaestchen" type="checkbox" name="interessen[]" id="dachstuehle" value="dachstuehle"     />
            <label style="display:inline;" for="holzbau">Holzbau</label>         <input class="kaestchen" type="checkbox" name="interessen[]" id="holzbau" value="holzbau" />
            <label style="display:inline;" for="innenausbau">Innenausbau</label> <input class="kaestchen" type="checkbox" name="interessen[]" id="innenausbau" value="innenausbau" />
            <label style="display:inline;" for="trocken">Trockenbau</label>      <input class="kaestchen" type="checkbox" name="interessen[]" id="trockenbau" value="trockenbau" /><br /><br />
            <input type="hidden" value="1" name="s" />
            <input type="submit" value="Nachricht versenden" name="submit" />
            </form>

What is wrong?

So the reason you're getting 'Bitte geben Sie Ihre Telefonnummer an.' is because $post['phone']; is empty or false or becomes empty/false after it's trimmed and the tags are stripped. Try echo'ing out $post['phone'] right before the if statement to see what it says. You may have an minor issue where your form is posting telephone even though you're checking for phone .

In addition to the $post['phone'] being empty you have an error on the preg match line. You're doing $phone when you need to do $this->phone :

} else if ( preg_match('#[^0-9\+\-\040/]#', $this->phone) )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM