Welcome to WebmasterWorld Guest from 23.22.140.143

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Limit number of characters in a field

     
10:41 am on Feb 12, 2007 (gmt 0)

Preferred Member

10+ Year Member

joined:Mar 7, 2003
posts:557
votes: 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

11:11 am on Feb 12, 2007 (gmt 0)

Junior Member

5+ Year Member

joined:Sept 27, 2006
posts: 54
votes: 0


<input type='text' name='keywords' size='60' value='##field##' maxlength='10' />
11:45 am on Feb 12, 2007 (gmt 0)

Preferred Member

10+ Year Member

joined:Mar 7, 2003
posts:557
votes: 0


Simple as that.

Thanks very much

Ska :O)

12:04 pm on Feb 12, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 12, 2005
posts:5966
votes: 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! :)

1:34 pm on Feb 12, 2007 (gmt 0)

Preferred Member

10+ Year Member

joined:Mar 7, 2003
posts:557
votes: 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

4:07 pm on Feb 12, 2007 (gmt 0)

Senior Member

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

joined:Mar 30, 2003
posts:3719
votes: 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

4:28 pm on Feb 12, 2007 (gmt 0)

Preferred Member

10+ Year Member

joined:Mar 7, 2003
posts:557
votes: 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

5:14 pm on Feb 12, 2007 (gmt 0)

Junior Member

5+ Year Member

joined:Sept 27, 2006
posts: 54
votes: 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.

8:40 pm on Feb 12, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member eelixduppy is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 12, 2005
posts:5966
votes: 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!

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members