Jump to content

stop users from entering too many chars.


stefsoko

Recommended Posts

How would I incorporate a function to simply check the "name" and "message" for a certain amount of chars, like 15 & 150?

 

<form method="post" action="chat.php">
<p><input name="name" type="text" id="name" value="your name" size="10" maxlength="15">
<input name="message" type="text" id="message" value="your message" size="20" maxlength="150">
<input name="submit" type="submit" id="submit"></p>
</form>
</body>
</html>
<?php

// when the submit button is clicked
if(isset($_POST['submit']))
{

// strip any html tags before continuing
$name=strip_tags($_POST['name']);
$message=strip_tags($_POST['message']);

// stop if nothing was entered
if($name!='')
if($message!='')
{

// trim any extra whitespace
$data=trim($name)."\n";
$data.=trim($message)."\n";

//open the text file and enter the data
$file_ar=file("db.txt");
$fp=fopen("db.txt","w");
fputs($fp,$data);
if($file_ar!=NULL)
{
$loop=0;
foreach($file_ar as $line)
{

// do not store more than 20 messages
if($loop>=19*3) break;
fputs($fp,$line);
$loop++;
}
}
fclose($fp);
}
}

// display the messages
$fp=fopen("db.txt","r");
while(!feof($fp))
{
$name=trim(fgets($fp,999));
$message=trim(fgets($fp,999));
if($name!='')
{
echo "<p><b>$name: </b>$message</p>";
}
}
fclose($fp);
?>

Link to comment
Share on other sites

The maxlength parameter will prevent the user from inputting more than the specified number of characters, so you shouldn't have to check for it.

 

...which can easily be circumvented.  You should never validate client-side.  You can find out the length of the string using strlen().  Make a condition that checks if string is greater than amount of chars you want to limit it by.

 

Link to comment
Share on other sites

The maxlength parameter will prevent the user from inputting more than the specified number of characters, so you shouldn't have to check for it.

 

...which can easily be circumvented.  You should never validate client-side.  You can find out the length of the string using strlen().  Make a condition that checks if string is greater than amount of chars you want to limit it by.

 

You may want to consider using both maxlength and strlen(). Client-side validation can be circumvented, but that doesn't mean you should never use it. Client-side validation has it's place but I agree that you shouldn't depend on it.

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.