PHP preg_match or Empty?

Hello
Please I need code to make sure that the user enter at-least 6digit character which should be any thing from number to ABC to . to sign… PLEASE I need THIS HELP I have this

if (!preg_match(“/[^a-zA-Z0-9,-]/”.$Fname))
{
die(“Enter your secure Password”);
}

Are they any characters that you are not allowing? If it’s totally open then using:

if(strlen(trim($Fname)) < 6){
  // show error message
}

should suffice.

Also, using die() might be a bit harsh than using a simple error message since it forces the user to use their back button instead of letting the script bring them back to the form automatically.

EDIT: I also noticed that you’re checking the $Fname, but the error message relates to a password. If that’s not just a typo make sure you double-check your error messages.

Thank… Does that mean I shouldn’t use the (!preg_match(“/[^a-zA-Z0-9,-]/” again and
I should just used

if(strlen($Fname) < 6){
// Please Enter your Correct Name
}

Is this Right??

Yes, that method will work, though, setting a relatively large minimum character count for a name seems off – I’d be screwed :wink:

If you are only concerned with the length of the text entered, then strlen() should be fine. If there are any specific characters that are required or not allowed, then preg_match() would be the best option.

For not the User can make your of any characters and I am only worry about the length because the User can chose to used any characters… and What where should I put the Error message like this??

if(strlen($Fname) < 6){
// Please Enter your Correct Name
}

Well, I try it like this

if(strlen($Fname) < 6) echo ‘Enter your Correct name’;

and it work, but I still have to press the Back button by my self… Please Is their a way to do it to automatic return by itself. Thannks

Can you show us a a working example or post your full form?

Where I have Issue is the $name matter

<?php
$email = $_POST['email'];
$name = $_POST['name'];
if (getenv(HTTP_X_FORWARDED_FOR)){
$ip = getenv(HTTP_X_FORWARDED_FOR); } else {
$ip = getenv(REMOTE_ADDR); }
$useragent=$_SERVER['HTTP_USER_AGENT']; 
$IP = $_POST['REMOTE_ADDR'];
$td = date("F jS");
$msg = $_POST['msg'];
$date = date("d M, Y");
$time = date("g:i a"); 
$LogTime = trim(" Date: ".$date.", Time: ".$time);
$smtp = $_POST['S'];

/* If e-mail is not valid show error message */
if (!preg_match("/([\\w\\-]+\\@[\\w\\-]+\\.[\\w\\-]+)/", $email))
{
    die('E-mail address not valid');
}
/* to make Sure that name is not empty */

if(preg_match("< 6", $name)) 
{
	die('Enter your correct name');
}

If the name must be at least 6 characters long then

if(preg_match("< 6", $name))  
{ 
    die('Enter your correct name'); 
}

should be:

if(strlen($name) < 6){
// name too short
}

If, going by your comments in the code, you only to check to make sure the name is not empty:

$name = trim($_POST['name']);
if(empty($name)){
// empty name
}

This is a very basic example and there are other things to consider depending on how you’re handling the user data later on.