Jump to content

How to Simply remove characters and spaces from a form text box.


Stalingrad

Recommended Posts

Hi all! I am building a website, and the users can create a pet name. I only want them to be able to use letters, numbers and underscores in the names. How can I do this? Thanks! I would also be using this for the usernames as well. Here is my pet creation code:

adopt.php:

<?php
session_start();
include("config536.php");
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>

<?php
if(!isset($_SESSION['username'])) {
echo "<banner></banner><ubar><a href=login.php>Login</a> or <a href=register.php>Register</a></ubar><content><center><font size=6>Error!</font><br><br>You are not Logged In! Please <a href=login.php>Login</a> or <a href=register.php>Register</a> to Continue!</center></content><content><center><font size=6>Messages</font><br><br></center></content>";
}

if(isset($_SESSION['username'])) {
echo "<banner></banner><nav>$shownavbar</nav><ubar>$ubear</ubar><content><center><font size=6>Adopt a Pet</font><br><br>";

$thedateis = date('F jS, Y');
$getpetid = $_GET['petid'];
$adopt = $_POST['submit'];
$petname = $_POST['petname'];
$petgen = $_POST['gender'];

$thebq = "SELECT * FROM pets WHERE petid='$getpetid'";
$theb = mysql_query($thebq);
while($prr = mysql_fetch_array($theb)) {
$pid = $prr['petid'];
$species = $prr['pname'];
$number = $prr['number'];
}

if(!isset($getpetid)) {
$pq = "SELECT * FROM pets WHERE petid != '0'";
$p = mysql_query($pq);
while($pr = mysql_fetch_array($p)) {
$pid = $pr['petid'];
$ptype = $pr['pname'];
$im = $pr['pimage'];
$number = $pr['number'];
$limited = $pr['limited'];
$types = $pr['types'];

echo "<b>$ptype</b><br><a href=?petid=$pid><img src=/images/pets/$im></a><br>Number: $number<br><br>";
}
}
if(isset($getpetid)) {
?>
<form action="<?php echo "$PHP_SELF"; ?>" method="POST">
Please select a Name: <input type="name" name="petname" maxlength="15"><br>
Pet Gender: <select name="gender"><option value="female">Female</option><option value="male">Male</option><br><br><input type="submit" name="submit" value="Adopt Pet"></form>
<?php
$checkitq = mysql_query("SELECT * FROM userpets WHERE userpetname='$petname'");
$gcheckit = @mysql_num_rows($checkitq);

$tq = "SELECT * FROM users WHERE username='$showusername'";
$t = mysql_query($tq);
while($rw = mysql_fetch_array($t)) {
$grabnum = $rw['pets'];
}
if(isset($adopt)) {
if($grabnum >= "4") {
echo "<font color=red>Error! You already have 4 pets.</font>";
}
if($petname == "") {
echo "<font color=red>Error! Please type in a name for your pet.</font>";
}
if($qcheckit == "1") {
echo "<font color=red>Error! That pet name already exists!</font>";
}
if($gcheckit == "0" && $petname != "" && $grabnum < "4") {
mysql_query("INSERT INTO userpets (owner, userpetname, userpetspecies, userpettype, petdatecreated, gender, strength, defence, health, level, booksread, smart, hunger) VALUES ('$showusername', '$petname', '$species', 'Normal', '$thedateis', '$petgen', '1', '1', '10', '1', '0', 'Normal', 'Full')");
mysql_QUERY("UPDATE users SET pets=pets+1 WHERE username='$showusername'");
echo "<font color=green>Success! Your pet has been created!</font>";
}
}


}
}
?>

</html>

 

Thanks in advance for the help everyone!!

Validate the input.

 

$field = trim($_POST['your_form_field']);
if( !ctype_alnum(str_replace('_', '', $field)) ) {
     // contains characters other than a-z, A-Z, 0-9 and _
}

ok im usign the following if statement, and it isn't working:

if( !ctype_alnum(str_replace('_', '', $field)) || $grabnum >= "4" || $petname == "" | $checkit == "1") {
echo "<font color=red>Error! Pet names must only have letters, numbers, or underscores.</font>";
}

look above to see my old code for what teh other statements are. how do I throw it all into one?

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.