Jump to content

Simple input filtering not so simple...


christofurr

Recommended Posts

I suck at writing PHP.

 

I'm trying to create a basic registration form, but I'm having trouble understanding how to allow and disallow specific characters in an text field; I want the Full Name field to allow only A-Z, a-z and spaces; I want the Username field to allow only A-Z, a-z, 0-9, and spaces; I want the Password field to allow only a-z and 0-9.

 

Can anyone explain in elemenary english how this can be accomplished??

 

P.S. - Did I mention that I suck at writing PHP?

Link to comment
https://forums.phpfreaks.com/topic/54682-simple-input-filtering-not-so-simple/
Share on other sites

After reading a bit about regular expressions, this is what I've come up with:

 

if (!ereg('([a-zA-Z0-9]+[:space:]*){3,32}', $string))
{
echo "Invalid characters in Username field.";
}
elseif (ereg('^[:space:]+', $string))
{
echo "Username cannot begin with a space.";
}
elseif (ereg('[:space:]+&', $string))
{
echo "Username cannot end with a space.";
}
else
{
echo "Your Username is " . $string;
}

 

If I wrote it correctly, it should display "Invalid characters in Username field" if there are any characters that aren't alphanumeric or spaces in $string, or if there are fewer than 3 or more than 32 characters in total. It should also display "Username cannot begin with a space" if $string starts with one or more spaces or "Username cannot end with a space" if $string ends with one or more spaces.

 

Did I make any errors?

Mmm... Parse error. Can't find my mistake.

 

<html>
<body>

<p>

<form action="form4.php" method="post">
Full Name: <input type="text" name="name" value="<?php echo $_POST["name"]; ?>" 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>

Your name is <?php echo $_POST["name"]; ?>. <br />
Your email address is <?php echo $_POST["email"]; ?>. <br />

</p>

<p>

<?php 
if (!filter_input(INPUT_POST, "email", FILTER_VALIDATE_EMAIL)
{
echo "Invalid email address.<br />";
}
if (!ereg('([a-zA-Z0-9]+[:space:]*){3,32}', $_POST["name"]))
{
echo "Invalid characters in Full Name field.";
}
elseif (ereg('^[:space:]+', $_POST["name"]))
{
echo "Full Name cannot begin with a space.";
}
elseif (ereg('[:space:]+&', $_POST["name"]))
{
echo "Full Name cannot end with a space.";
}
else
{
echo "Your Full Name is " . $_POST["name"];
}

?>

</p>

</body>
</html>

It helps if you indent your code so its readable.

 

<?php 

  if (!filter_input(INPUT_POST, "email", FILTER_VALIDATE_EMAIL) {
    echo "Invalid email address.<br />";
  } elseif (!ereg('([a-zA-Z0-9]+[:space:]*){3,32}', $_POST["name"])) {
    echo "Invalid characters in Full Name field.";
  } elseif (ereg('^[:space:]+', $_POST["name"])) {
    echo "Full Name cannot begin with a space.";
  } elseif (ereg('[:space:]+&', $_POST["name"])) {
    echo "Full Name cannot end with a space.";
  } else {
    echo "Your Full Name is " . $string;
  }

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.