Limit the maximum length of submitted data

Here's one way of many of making sure that user submitted data does not exceed a maximum allowed length (set by the form author) in a form control.

Say there's the scenario that requires you to ensure that when someone comments on your blog post, they do not enter more than 25 characters (you're not looking for detailed feedback on your post!).

The approach demonstrated here is to disable the button the user needs to press to submit their comment unless the text they enter is less than or equal to 25 characters. If they do enter more than that, we disable the button and inform them that they need to shorten their comment. It's also nice to let them know how many characters they have left to enter too.

The form below implements all of the requirements described:

Comment on this article: characters remaining. Post comment
Your comment is too long! Please shorten it.

The comment form consists of a few controls. A textarea to allow text to be entered, an output to show how many more characters are able to be entered.

  1. <xf:textarea ref="comment" incremental="true">
  2. <xf:label>Comment on this article:</xf:label>
  3. ...
  4. </xf:textarea>
  5. <xf:output ref="state/@chars-remaining"></xf:output>
  6. <span class="substitute-label-text">characters remaining</span>.

The two controls reference some instance data:

  1. ...
  2. <data xmlns="">
  3. <comment></comment>
  4. <state max-length="25" chars-remaining=""></state>
  5. </data>
  6. ...

The 'comment' node is referenced by the textarea control and holds the users input. The 'state/@chars-remaining' attribute is a calculated value referenced by the output control to show how many characters are still able to be input (we'll explain that in a minute). The 'state/@max-length' value is hard-coded and is what sets the maximum allowed length of the users input. It could be changed to a higher or lower value to allow more or less text to be input.

The button that allows the user to submit their short comment is contained in an XForms switch/case element. There are two cases, one to hold the button, one to hold the message to show the user when they've typed too much.

  1. <xf:switch>
  2. <xf:case id="comment-ok">
  3. <xf:trigger><xf:label>Post comment</xf:label></xf:trigger>
  4. </xf:case>
  5. <xf:case id="comment-disallowed">
  6. <div class="error-text">Your comment is too long! Please shorten it.</div>
  7. </xf:case>
  8. </xf:switch>

To switch cases when too much text is entered, we set up a validity constraint on the 'comment' node, using an xf:bind element. This enables us to tell when the control becomes valid/invalid and this means that we can then use the xforms-valid and xforms-invalid events to toggle the two cases on and off.

Firstly the validity constraint for the 'comment' node. This says the node will be valid up until more than the '@max-length' number of characters has been entered:

  1. <xf:bind
  2. nodeset="comment"
  3. constraint="string-length(.) &lt;= ../state/@max-length"
  4. ></xf:bind>

then, we use the valid/invalid events by modifying the textarea control markup to toggle the cases:

  1. <xf:textarea ref="comment" incremental="true">
  2. <xf:label>Comment on this article:</xf:label>
  3. <xf:toggle ev:event="xforms-valid" case="comment-ok"></xf:toggle>
  4. <xf:toggle ev:event="xforms-invalid" case="comment-disallowed" ></xf:toggle>
  5. </xf:textarea>

The only unexplained part of the form is the output control that informs us how many characters are still available to be input. I mentioned earlier that this is a calculated value. It's calculated using another xf:bind element with a @calculate attribute. This subtracts the length of the string the user has entered at a particular point from the maximum allowed (state/@max-chars) value.

  1. <xf:bind
  2. nodeset="state/@chars-remaining"
  3. calculate="number(../@max-length) - string-length(../../comment)"
  4. ></xf:bind>

Each time the user types a character the @calculate equation gets re-evaluated because of the @incremental="true" attribute that's part of the textarea control. This is important to note as, if the @incremental value was 'false' or the attribute omitted entirely, the equation would only get evaluated once the user had navigated away from the control.

That's pretty much it apart from a bit of CSS that styles the controls colour/appearance. I'm sure you can work that out though! An exercise for the reader might be to modify the form to ensure that not only is there a maximum number of characters entered but also a minimum number too.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Thank you for being so

Thank you for being so informative all the way, appreciate every little thing you share, just keep updating here.
web hosting

Great, lots of good advice on

Great, lots of good advice on your post. and as a return,i will buy a NFL Jerseys to you for my thanking.

How long have you been in

How long have you been in this field? You seem to know a lot more than I do, I’d love to know your sources!

injury lawyer

it’s good to see this

it’s good to see this information in your post, i was looking the same but there was not any proper resource.
Online Writing | dissertation

I think youve made some truly

I think youve made some truly interesting points. Not too many people would actually think about this the way you just did. Im really impressed that theres so much about this subject thats been uncovered and you did it so well, with so much class. Good one you, man! Really great stuff here.
simulation assurance auto

Thanks for the link to this

Thanks for the link to this resource.wedding bands | beads

Wow...this module is pretty

Wow...this module is pretty helpful for me to control the length of submitted data....
free advertising |job listings|tempurpedic

Each time the user types a

Each time the user types a appearance the calculate blueprint pass4sure 70-431 gets re-evaluated This is important to agenda as, if the incremental amount was 'false' pass4sure 220-702 or the aspect bare entirely, the blueprint would alone get evaluated already the user had navigated abroad from the control pass4sure 642-982. That's appealing abundant it afar from a bit of CSS that styles the controls colour/appearance. I'm abiding you can plan that out though pass4sure 642-524! An exercise for the clairvoyant ability be to adapt the anatomy to ensure that not alone is there a best amount of characters entered but aswell a minimum amount too.

This snippet is useful, i'll

This snippet is useful, i'll use it for the comment section of my website incorporation quebec.

Search marketing - Welcome to

Search marketing - Welcome to the Search Marketing Group, a search marketing company specialising in SEO and search marketing in the UK.

Some useful code - I have

Some useful code - I have adapted this for username and password forms - and may use it on address forms as sometimes they end up filled with a lot of junk.
heat resistant tape

This is a nice addition to

This is a nice addition to the platform - shame it wasn't included by default!
music for meditation

Limiting the bandwith

Limiting the bandwith allocated to that is smart. I also like askimet for wordpress to do that, I use it for my website about dog tags for dogs.

I read your post . it was

I read your post . it was amazing.Your thought process is wonderful.
The way you tell about things is awesome. i always wait for your posts.
They are inspiring and helpful.Thanks for sharing your information
Discount Express

Does anyone know of an Apache

Does anyone know of an Apache or PHP config setting that controls the quantity of POST variables allowed to be submitted?

I have a simple data input webpage that has been working fine for months. The webpage consists of 19 lines of 12 text input fields, plus two hidden fields. The user fills in the input fields and Nightlifeblues
presses submit.

this module is pretty helpful

this module is pretty helpful for me to control the length of submitted data....Link Building

Thanks, however this is not

Thanks, however this is not what I need. The system is much more sophisticated than that. First of all, extension check is not acceptable, and second of all, you can get an exe file rename it as a txt; however when you upload it it's still an exe.
I prefer to limit it as well to control how much bandwith is spent. I do so on my website Builders4u pretty well.

When it does exceed a maximum

When it does exceed a maximum allowed length. You definitely run into a lot of problem. I think these tips will definitely help me.

beach vacation

Putting limitations on a file

Putting limitations on a file upload in ColdFusion is a bit tricky. The problems with something like that is that we can't really figure out how large the file is until it is fully uploaded (at least as far as I know - I have never seen a good upload progress bar in ColdFusion). You can do hacks using Javascript to get a File System Object, but those are a huge security risks and will prompt the user for approval. In order to make the processing as transparent as possible, we are going to have to handle it all on the server side.

I prefer to limit it as well

I prefer to limit it as well to control how much bandwith is spent. I do so on my website dog tags for dogs pretty well.

People always look for this

People always look for this type of good information.
I hope that it will helpful for needy people.I am apreciating it very much!
Looking forward to another great article. Good luck to the author! all the best!
Cleaning Services

Really interesting. I've been

Really interesting. I've been doing some studying on data for the past months. I'm really thinking of pursuing a career on it. Part time at least. I can't say that I comprehend what is posted here, but I think there's no need for dissertation help. Or any kind of reference for that matter. Thanks for posting.

Whilst the Developer Zone

Whilst the Developer Zone will be unashamedly biased towards XForms, within that, we'll have discussions, articles, code snippets, ...placement argent

thanks for article adim. Nice

thanks for article adim. Nice post. Guzel codeler sagol uygulicağim Sohbet odaları dediklerinizi.chat

it does exceed a maximum

it does exceed a maximum allowed length. You definitely run into a lot of problem. I think these tips will definitely help me.link building service

extension check is not

extension check is not acceptable, and second of all, you can get an exe file rename it as a txt; however when you upload it it's still an exe.yeast infection cure

How can I set the maxlength

How can I set the maxlength of input (text) via xforms? Any idea? Thx.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.