/    Sign up×
Community /Pin to ProfileBookmark

number of chararcters…

Hi There.

I would like to check if the user entered more then 200 chararcters and if so tell him that it’s too much.
how can I do it?

Thanks

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@Paul_JrFeb 23.2004 — Something like this?
[size=1]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head><title>Example</title>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=iso-8859-1" />

<script type="text/javascript">
//<![CDATA[
function check(Obj, Objmax) {
if(Obj.value.length == Objmax) {
alert("Character limit reached.");
}
}
//]]>
</script>

</head>
<body>

<form action="#">
<div>
<label for="username">Username: <br />
<input type="text" name="username" id="username" maxlength="200" onkeypress="check(this, this.maxLength);" />
</label>
</div>
</form>

</body>
</html>
[/size]
Copy linkTweet thisAlerts:
@weeeauthorFeb 24.2004 — That I can't get it workwith TEXTAREA, why?
Copy linkTweet thisAlerts:
@Paul_JrFeb 24.2004 — <textarea> doesn't support the maxlength attribute, that's why it won't work. Unfortunately, you'll have to talk to someone else about this, as it's beyond my muddling scope.

I'll see what I can dig up, but you might want to PM Fredmv, or Olerag, or someone JS inclined.
Copy linkTweet thisAlerts:
@Paul_JrFeb 24.2004 — Here, try this. It's kinda smushed together, and you have to specifiy the max length you want, plus 1. So for you it would be 201.
[size=1]
&lt;script type="text/javascript"&gt;
//&lt;![CDATA[
function check(Obj, Objmax) {
var maxnum = Obj.value.length;
if(Obj.value.length &gt;= Objmax) {
alert("Character limit reached.");
Obj.value = Obj.value.slice(0, maxnum);
}
}
//]]&gt;
&lt;/script&gt;
... ... ...
&lt;form action="#"&gt;
&lt;div&gt;
&lt;label for="username"&gt;Username: &lt;br /&gt;
&lt;textarea id="username" maxlength="2" cols="60" rows="10" onkeydown="check(this, '201');"&gt;&lt;/textarea&gt;
&lt;/label&gt;
&lt;/div&gt;
&lt;/form&gt;
[/size]

I'm quite sure there's a better way, but that seems to work.
Copy linkTweet thisAlerts:
@weeeauthorFeb 24.2004 — it's not good as the other one you showed me.

Thanks man
Copy linkTweet thisAlerts:
@Paul_JrFeb 25.2004 — [i]Originally posted by weee [/i]

[B]it's not good as the other one you showed me.



Thanks man [/B]
[/QUOTE]

Explain?

The other one was technically better, yes, because the character limit was specified using the [FONT=courier new]maxlength[/FONT] atrribute. So, even with JS disabled, the user is only allowed to type in 200 characters -- they just don't get the alert. But since [FONT=courier new]<textarea></textarea>[/FONT] doesn't support that attribute, that affect had to be emulated with JS, therefore users without JS will be able to type in as many characters as they so desire.

Actually, you don't need to specify the limit you want plus one. :o
Copy linkTweet thisAlerts:
@weeeauthorFeb 25.2004 — with emulated JS?
Copy linkTweet thisAlerts:
@Paul_JrFeb 25.2004 — [i]Originally posted by weee [/i]

[B]with emulated JS? [/B][/QUOTE]

Last I heard 13% of people browsing the internet don't have JS enabled/available.
Copy linkTweet thisAlerts:
@weeeauthorFeb 25.2004 — that I can find information like what people using as browsers, Mac or PC, reulotion and ect?

Thanks
Copy linkTweet thisAlerts:
@Paul_JrFeb 25.2004 — [i]Originally posted by weee [/i]

[B]that I can find information like what people using as browsers, Mac or PC, reulotion and ect?



Thanks [/B]
[/QUOTE]

I don't know of any, sorry.
Copy linkTweet thisAlerts:
@AdamGundryFeb 25.2004 — Check out the options down the right hand side:

http://www.thecounter.com/stats/2004/January/index.php

Adam
×

Success!

Help @weee 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.27,
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,
)...