Jump to content

only numbers or letters acepted in form field


adhemar

Recommended Posts

Next form is working, buth when i enter the "nr", then I have an error message. The raison is that i can fill in only letters or only nubers. This is the number of an address.

When i enter only a number, i receive the mail, but with delay.

Can anyone help me with the code?

 

 

<?php
if(isset($_POST['email'])) {
     
    $email_to = "mijn@email.be";
    $email_subject = "Mijn Formulier";

    function died($error) {
        // your error code can go here
        echo "Sommige velden zijn niet ingevuld.<br /><br />";
        //echo "De fouten staan hieronder.<br /><br />";
        echo $error."<br /><br />";
        echo "Ga terug en verbeter de fouten.<br /><br />";
        die();
    }
     
    // validation expected data exists
    if(!isset($_POST['voornaam']) ||
        !isset($_POST['naam']) ||
        !isset($_POST['straat']) ||
        !isset($_POST['nr']) ||
        !isset($_POST['postcode']) ||
        !isset($_POST['plaats']) ||
        !isset($_POST['email']) ||
        !isset($_POST['aantal']) ||
        !isset($_POST['petitie'])) {
        died('Alle velden moeten ingevuld worden');      
    }
     
    $voornaam = $_POST['voornaam']; // required
    $naam = $_POST['naam']; // required
    $straat = $_POST['straat']; // required
    $nr = $_POST['nr']; // required
    $postcode = $_POST['postcode']; // required
    $plaats = $_POST['plaats']; // required
    $email = $_POST['email']; // required
    $aantal = $_POST['aantal']; // not required
    $petitie = $_POST['petitie']; // required
     
    $error_message = "";
//    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
//  if(!preg_match($email_exp,$email)) {
//    $error_message .= 'Het email adres is niet correct.<br />';
// }
    $string_exp = "/^[A-Za-z0-9.'-]+$/";
  if(!preg_match($string_exp,$voornaam)) {
    $error_message .= 'De voornaam moet ingevuld worden.<br />';
  }
  if(!preg_match($string_exp,$naam)) {
    $error_message .= 'De naam moet ingevuld worden.<br />';
  }

  if(!preg_match($string_exp,$straat)) {
    $error_message .= 'De Straat moet ingevuld worden.<br />';
  }
  if(!preg_match($string_exp,$nr)) {
    $error_message .= 'Het Nr moet ingevuld worden.<br />';
  }
  if(!preg_match($string_exp,$postcode)) {
    $error_message .= 'De Postcode moet ingevuld worden.<br />';
  }
  if(!preg_match($string_exp,$plaats)) {
    $error_message .= 'De Gemeente moet ingevuld worden.<br />';
  }
  if(!preg_match($string_exp,$aantal)) {
    $error_message .= 'Het Aantal inwoners moet ingevuld worden.<br />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Krottegemsemarkten Petitie\n\n";
     
    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }
     
    $email_message .= "Petitie: ".clean_string($petitie)."\n\n";
    $email_message .= "Naam: ".clean_string($naam)."\n\n";
    $email_message .= "Voornaam: ".clean_string($voornaam)."\n\n";
    $email_message .= "Adres: ".clean_string($straat)."\n\n";
    $email_message .= "Nr: ".clean_string($nr)."\n\n";
    $email_message .= "Postnr: ".clean_string($postcode)."\n\n";
    $email_message .= "Plaats: ".clean_string($plaats)."\n\n";
    $email_message .= "Email: ".clean_string($email)."\n\n";
    $email_message .= "Aantal inwoners op dit adres: ".clean_string($aantal)."\n\n";

// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);

header('Location: http://mijnsite.be/bedankt.html');
?>

<?php
}
?>

 

Thanks in advance,

Adhemar

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.