Welcome to WebmasterWorld Guest from 54.166.224.46

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)

10+ Year Member



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)

5+ Year Member



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

10+ Year Member



Simple as that.

Thanks very much

Ska :O)

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

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



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)

10+ Year Member



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)

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



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)

10+ Year Member



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)

5+ Year Member



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)

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



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!

 

Featured Threads

Hot Threads This Week

Hot Threads This Month