homepage Welcome to WebmasterWorld Guest from 54.166.53.169
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Limit number of characters in a field
ska_demon

10+ Year Member



 
Msg#: 3249935 posted 10:41 am on Feb 12, 2007 (gmt 0)

Please forgive me as I am not a programmer by any stretch.

My directory uses this piece of code to produce a field in a form.

<td><input type=text name=keywords size=60 value='##field##'>

Please could someone tell me how to limit the number of characters allowed in that field.

That's it

Thanks

Ska

 

ericjust

5+ Year Member



 
Msg#: 3249935 posted 11:11 am on Feb 12, 2007 (gmt 0)

<input type='text' name='keywords' size='60' value='##field##' maxlength='10' />

ska_demon

10+ Year Member



 
Msg#: 3249935 posted 11:45 am on Feb 12, 2007 (gmt 0)

Simple as that.

Thanks very much

Ska :O)

eelixduppy

WebmasterWorld Senior Member eelixduppy us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 3249935 posted 12:04 pm on Feb 12, 2007 (gmt 0)

This solution will work, but you cannot be 100% sure that it will be that length. To be 100%, you must check again server-side. Something like this would work:

$maxlength = 20;
$string = $_POST['string'];
if([url=http://us2.php.net/manual/en/function.strlen.php]strlen[/url]($string) > $maxlength) {
$string = [url=http://us2.php.net/substr]substr[/url]($string, 0, $maxlength);

Good luck! :)

ska_demon

10+ Year Member



 
Msg#: 3249935 posted 1:34 pm on Feb 12, 2007 (gmt 0)

Thanks eelix

I checked the form and the user physically cannot type more than the allowed number characters. I will go for the easy option that I understand at the moment and if there are any problems I shall move forward.

Thanks for your help guys/girls

Ska

dreamcatcher

WebmasterWorld Senior Member dreamcatcher us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3249935 posted 4:07 pm on Feb 12, 2007 (gmt 0)

ska_demon,

Its worth pointing out that if people have things installed like Firefox`s web dev toolbar, the max length can be disabled.

dc

ska_demon

10+ Year Member



 
Msg#: 3249935 posted 4:28 pm on Feb 12, 2007 (gmt 0)

Oh

Ok Thanks dreamcatcher.

Dammit why do people have to be so damn twisty n clever all the time ;oP

I hate programmers

Thanks again

Ska

ericjust

5+ Year Member



 
Msg#: 3249935 posted 5:14 pm on Feb 12, 2007 (gmt 0)

It depends on why you need to limit the length. Are you storing the value in a database? Does the script that stores it in the database truncate the string (have a maximum length?).

It's probably a fine solution to just use the maxLength attribute. If someone wants to intentionally remove the limit and enter more characters, then why not let them? I'm sure that the value is being checked (or limited by the DB) before it's stored.

eelixduppy

WebmasterWorld Senior Member eelixduppy us a WebmasterWorld Top Contributor of All Time 5+ Year Member



 
Msg#: 3249935 posted 8:40 pm on Feb 12, 2007 (gmt 0)

Yes, dreamcatcher hit it right on the head.


I hate programmers

hehe...it's not our faults ;)

In case you decide to go with my little code snippet above, make sure to place a closing bracket at the end as I completely forgot to!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved