/    Sign up×
Community /Pin to ProfileBookmark

How to count maximum values inside text box?

I like to count number of characters inside text box.
1. How to do it with PHP to echo it or/and
2. limit it to maximum with warning and/or
3. how to set it with automatic Javascript code when user filled text box?

Need help.

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@007JulienDec 15.2011 — In my opinion, the best solution consists in using regular expressions...

With javascript, the single following lines count characters (we remove all other characters before).
[CODE]
// For lower or upper case letters

var result=str.replace(/[^a-z]+/gim,'').length;
// For lower word characters depending of the local setting

var result=str.replace(/[W]+/gim,'').length;
// For lower word or digits characters

var result=str.replace(/[WD]+/gim,'').length;
[/CODE]


It is the same thing with PHP
[code=php]$str=preg_replace("/[^a-z]+/gi","",$str);
$result=strlen($str);// Or mb_strlen($str); with character encoding[/code]
×

Success!

Help @toplisek spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...