Jump to content

Recommended Posts

>:(

 

PHP is getting me fustrated.

 

I'm trying to code a simple form and validation. The email validation is working fine, as far as I've tested it. But the name validation isn't giving me the output I'm trying to get it to give me.

 

Here's what I hope for:

~ If the Nickname input has characters other than a-z, A-Z, and spaces, fail.

~ If the Nickname input has fewer than 3 characters, fail.

~ If the Nickname input has more than 32 characters, fail.

~ If the Nickname input starts with a space, fail.

~ If the Nickname input ends with a space, fail.

~ Otherwise, succeed.

 

And I get:

~ Success with Nickname input like "tyeu!@@##$%^%^&*())" <-- Invalid characters.

~ Success with Nickname input like "12345678901234567890123456789012345" <-- 35 characters.

~ Success with Nickname input like "Choosh " <-- Space at the end.

~ Failure with Nickname input that begin with the lowercase characters a, c, e, p, and s.

 

It might be a bit sloppy (not sure how to neaten by indenting), but here's the code:

<html>
<head>
</head>
<body>

<p>
<form action="form7.php5" method="post">
Nickname: <input type="text" name="nickname" value="<?php echo $_POST["nickname"]; ?>" size="35" 

maxlength="35" />
Email Address: <input type="text" name="email" value="<?php echo $_POST["email"]; ?>" size="35" 

maxlength="50" />
<input type="submit" value="Create Account" />
</form>
</p>

<p>
<?php
if (!empty($_POST))
{
if (empty($_POST["nickname"]))
{
echo "You forgot to tell us what you want your nickname to be.<br />";
}
else
{
echo "You requested for your nickname to be <b>" . $_POST["nickname"] . "</b>.<br />";
if (!ereg('([a-zA-Z0-9]+[:space:]*){1,33}', $_POST["nickname"]))
{
echo "Your nickname can only consist of alphanumeric characters (A-Z & 0-9) and spaces. Please 

check your input.<br />";
}
elseif (!ereg('([a-zA-Z0-9]+[:space:]*){3,}', $_POST["nickname"]))
{
echo "Your nickname must consist of at least three characters.<br />";
}
elseif (!ereg('([a-zA-Z0-9]+[:space:]*){1,32}', $_POST["nickname"]))
{
echo "Your nickname cannot consist of more than 32 characters.<br />";
}
elseif (ereg('^[:space:]+', $_POST["nickname"]))
{
echo "Your nickname cannot begin with a space.<br />";
}
elseif (ereg('[:space:]+&', $_POST["nickname"]))
{
echo "Your nickname cannot end with a space.<br />";
}
else
{
echo "This nickname is available.<br />";
}}
if (empty($_POST["email"]))
{
echo "You forgot to tell us your email address.<br />";
}
else
{
echo "You told us that your email address is <b>" . $_POST["email"] . "</b>.<br />";
if (!filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL))
{
echo "The email address <b>" . $_POST["email"] . "</b> does not exist. Please check your 

input.<br />";
}
else
{
echo "You may register with this address.<br />";
}}}
?>

</p>

</body>
</html>

 

 

I can definitely see where you could shorten your code and make it more efficient. For two form fields you have an awful lot of code there :)

 

Examples:

 

Change the echo statements to an array of error messages that you can display. So instead of this:

 

if (empty($_POST["nickname"]))
{
echo "You forgot to tell us what you want your nickname to be.<br />";
}

 

You'd do something like this:

 

<?php
$errors = array[];

if(empty($_POST['nickname']))
  $errors = "You did not enter a nickname. Please complete that field.";

 

That way you can loop through all the errors at one time instead of individually. Conceivably someone could have to resubmit your form a half dozen times to get through all the errors one at a time. To loop them you'd insert this just above your form code:

 

<?php

if(!empty($errors)) {
echo "<font color='red'><strong id='errorTitle'>One or more input fields on the form has not been completed</strong>";
echo "<ul>";
foreach ($errors as $value) {
echo "<font face='Verdana' size='2' color='red'><li>$value</li></font><br/>";
} 
echo "</ul>";
}
?>

 

That way it would list all their errors at once in a list format.

 

Also, you can consolidate some of your 'if' statements. For example, instead of this:

 

if (empty($_POST["nickname"]))
{
echo "You forgot to tell us what you want your nickname to be.<br />";
}
else
{
echo "You requested for your nickname to be <b>" . $_POST["nickname"] . "</b>.<br />";
if (!ereg('([a-zA-Z0-9]+[:space:]*){1,33}', $_POST["nickname"]))
{
echo "Your nickname can only consist of alphanumeric characters (A-Z & 0-9) and spaces. Please 

check your input.<br />";

 

You might do this:

 

if (empty($_POST["nickname"]) || if (!ereg('([a-zA-Z0-9]+[:space:]*){1,33}', $_POST["nickname"])))
  $errors = "The nickname field was empty or did not match our requirements. Nicknames must be between 3 and 35 characters, contain letters A-Z, numbers 0-9 and can contain spaces. Please try again.";

 

This means 'either or' fails and the error displays.

I'm not really trying to condense to code, nor make all failure messages display at one time. I want it to display the failures when they're supposed to be displayed based on the conditions I used. For example, when a user submits a 35 character long nickname, it should display "Your nickname cannot consist of more than 32 characters."

 

Aside from that, I discovered another problem. The failure is displayed with input like "x y" or "1 2", displaying the "Your nickname must consist of at least three characters" message as if it ignores the space between the two letters.

Use trim to remove spaces, no need to generate an error message.

 

Use strlen to determine the length of a string. Regex are NOT the be all and end all.

 

Your really going the overkill method here.

 

Oh.. and indentation is simple.

 

<?php

  if (statement) {
    // do something
  } else {
    // do something else.
    if (statement) {
      // do something.
    } else {
      // do something else.
    }
  }

OK, Thorpe. That would surely solve the problems I have with the the max length and the spaces starting and ending input. But what about the other three issues?

 

~ Success with Nickname input like "tyeu!@@##$%^%^&*())".

~ Failure with Nickname input composed of two characters with a space in between.

~ Failure with Nickname input that begin with the lowercase characters a, c, e, p, and s.

Here's the new code:

 

<html>
<head>
</head>
<body>

<p>
<form action="form8.php5" method="post">
Nickname: 
	<input type="text" name="nickname" value="<?php echo $_POST["nickname"]; ?>" size="35" maxlength="35" />
Email Address: 
	<input type="text" name="email" value="<?php echo $_POST["email"]; ?>" size="35" maxlength="50" />
	<input type="submit" value="Create Account" />
</form>
</p>

<p>
<?php
$nicktrim = trim($_POST["nickname"]);

if (!empty($_POST))
{if (empty($_POST["nickname"]))
	{echo "You forgot to tell us what you want your nickname to be.<br />";}
else
	{echo "You requested for your nickname to be <b>" . $nicktrim . "</b>.<br />";
	if (!ereg('([:space:]*[a-zA-Z0-9]+[:space:]*)+', $nicktrim))
		{echo "Your nickname can only consist of alphanumeric characters (A-Z & 0-9) and spaces. Please check your input.<br />";}
	elseif (strlen($nicktrim<3))
		{echo "Your nickname must consist of at least three characters.<br />";}
	elseif (strlen($nicktrim>32))
		{echo "Your nickname cannot consist of more than 32 characters.<br />";}
	else
		{echo "This nickname is available.<br />";}
if (empty($_POST["email"]))
	{echo "You forgot to tell us your email address.<br />";}
else
	{echo "You told us that your email address is <b>" . $_POST["email"] . "</b>.<br />";
	if (!filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL))
		{echo "The email address <b>" . $_POST["email"] . "</b> does not exist. Please check your input.<br />";}
	else
		{echo "You may register with this address.<br />";}}}
?>

</p>

</body>
</html>

Made a few corrections to the new code and everything works except the ereg function.

 

if (!ereg('([:space:]*[a-zA-Z0-9]+[:space:]*)+', $nicktrim))
         {echo "Your nickname can only consist of alphanumeric characters (A-Z & 0-9) and spaces. Please check your input.<br />";}

 

It's supposed to disallow nicknames with anything other than letters, numbers, and spaces, but it accepts input like ~!@#$%^&*()_+.

Oh, I understand. I thought that eregi('[^a-zA-Z0-9 ]', $nicktrim) would match strings that begin with those characters. I looked it up though. That will probably work, but do I need to put a-z and A-Z although eregi ignores case distinction? Also, is the space after the 0-9 the same as [:space:]?

Oh, I understand. I thought that eregi('[^a-zA-Z0-9 ]', $nicktrim) would match strings that begin with those characters. I looked it up though. That will probably work, but do I need to put a-z and A-Z although eregi ignores case distinction? Also, is the space after the 0-9 the same as [:space:]?

no it match that have character that is not one of this 'a-zA-Z0-9 '

' ' is space

you are right don't need both a-z and A-Z

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.