Doug Posted 5 hours ago Share Posted 5 hours ago I get an error messages when inputting 'my name' on form but 'myname'(no space) is fine. One work around is obviously to sperate fields first name and surname but surely there is a method where only one box is needed? I've been playing around with $validation_filters but nothing seems to have any effect. I include the whole script below incase i have missed some other part of the script that should be tweaked. Also I am aware that the 'tel' code is not right as it will not accept '0' but I now know tel is not a number but a a string. I hope to resolve this myself but the pressing problem ATM is allowing a simple name to be input with a space between forename and surname! Or do they have to be separate items on the form? All help appreciated! ?php // 1. SETUP $user = ['name' => '', 'tel' => '', 'email' => '', 'comment' => '']; $errors = ['name' => '', 'tel' => '', 'email' => '', 'comment' => '']; $message = ''; $form_submitted_successfully = false; // A flag to know when to hide the form // 2. PROCESS FORM IF SUBMITTED if ($_SERVER['REQUEST_METHOD'] == 'POST') { // Validation filters $validation_filters['name']['filter'] = FILTER_VALIDATE_REGEXP; $validation_filters['name']['options']['regexp'] = '/^[A-z]{2,30}$/'; $validation_filters['tel']['filter'] = FILTER_VALIDATE_INT; $validation_filters['email']['filter'] = FILTER_VALIDATE_EMAIL; $validation_filters['comment']['filter'] = FILTER_VALIDATE_REGEXP; $validation_filters['comment']['options']['regexp'] = '/^[A-z]{2,400}$/'; $user_input = filter_input_array(INPUT_POST, $validation_filters); // Create error messages $errors['name'] = $user_input['name'] ? '' : 'Name must be 2-30 letters using A-Z'; $errors['tel'] = $user_input['tel'] ? '' : 'Please enter a valid telephone number'; $errors['email'] = $user_input['email'] ? '' : 'Please enter a valid email address'; $errors['comment'] = $user_input['comment'] ? '' : 'Please add a valid comment'; // Sanitize the original POST data to redisplay it safely in the form $user['name'] = filter_var($_POST['name'], FILTER_SANITIZE_FULL_SPECIAL_CHARS); $user['tel'] = filter_var($_POST['tel'], FILTER_SANITIZE_NUMBER_INT); $user['email'] = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL); $user['comment'] = filter_var($_POST['comment'], FILTER_SANITIZE_FULL_SPECIAL_CHARS); // Check if there are any errors by joining all error messages $invalid = implode($errors); // 3. DECIDE WHAT TO DO NEXT if ($invalid) { // If there are errors, show an error message $message = 'Please correct the following errors:'; } else { // If data is valid, SEND THE EMAIL $to = '[email protected]'; $subject = 'Contact Form Submission'; $msg = "Name: {$user['name']}\n" . "Tel: {$user['tel']}\n" . "Email: {$user['email']}\n" . "Comment: {$user['comment']}\n"; $headers = '[email protected]'; // The mail() function returns true on success, false on failure if (mail($to, $subject, $msg, $headers)) { $message = 'Thank you, your data has been sent!'; $form_submitted_successfully = true; // Set flag to true ?> <a href="formworking2.php">Go to form</a> <?php } else { $message = 'Sorry, there was an error sending your message. Please try again later.'; } } } ?> <!DOCTYPE html> <html lang="en"> <head> <link rel="stylesheet" type="text/css" href="doug.css" media="all and (min-width: 1300px)" /> <title>Validation Form</title> </head> <body> <h1>Contact Us</h1> <?php if ($message): ?> <p class="message"><?= $message ?></p> <?php endif; ?> <?php // Only show the form if it hasn't been submitted successfully if (!$form_submitted_successfully): ?> <form name="form" action="" method="POST"> Name: <input type="text" name="name" value="<?= htmlspecialchars($user['name']) ?>"> <span class="error"><?= $errors['name'] ?></span><br> Tel: <input type="text" name="tel" value="<?= htmlspecialchars($user['tel']) ?>"> <span class="error"><?= $errors['tel'] ?></span><br> Email: <input type="text" name="email" value="<?= htmlspecialchars ($user['email']) ?>"> <span class="error"><?= $errors['email'] ?></span><br> Comment: <textarea name="comment" rows="5" cols="40" input type="text" ($user['comment']) ?></textarea> <span class="error"><?= $errors['comment'] ?></span><br> <br><br> <input type="submit" value="Submit"> </form> <?php endif; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/330416-can-validation_filters-accept-spaces/ Share on other sites More sharing options...
kicken Posted 2 hours ago Share Posted 2 hours ago (edited) You are attempting to validate using a regular expression. Use a tool like Regex101 to help you develop and understand your expression. This is your current expression: /^[A-z]{2,30}$/ What this says that your string must contain only characters in the range from uppercase A to lowercase z, at least 2 times, but up to 30 times. If you want to include spaces in that, just add a space to the list of acceptable characters (in the brackets). /^[A-z ]{2,30}$/ Some additional notes: The range A-z probably doesn't do what you think it does. Each character is associated with a number. Uppercase A is 65, lowercase z is 122. What A-z does is allow any character within that range. That includes all upper and lowercase letters, but it also includes some punctuation characters: [\]^_`. So with your current regular expression, the name ^_^ would be accepted. Two ways to avoid this are to either list A-Z and a-z separately, or use the case-insensitive flag. Your current filtering method would not allow "international" characters, like ä, so a name like Märta would be rejected. This can be fixed by using a unicode escape sequence rather than ascii range. You probably shouldn't both validating a name outside of a maximum length (and 30 seems a bit short) Edited 2 hours ago by kicken Quote Link to comment https://forums.phpfreaks.com/topic/330416-can-validation_filters-accept-spaces/#findComment-1659444 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.