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
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?

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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;
  }

?>

Link to comment
Share on other sites

 

even a correct code in one big line will not give a parse error, indents helps to read the code and understand easily.....

 

the extra brace need not be on that line, some brace would have added or moved before that....

let me check that too..

 

~J

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.