File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes Acess text fields for which maxlength is defined Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Acess text fields for which maxlength is defined" Watch "Acess text fields for which maxlength is defined" New topic
Author

Acess text fields for which maxlength is defined

SaurabhSri Sri
Ranch Hand

Joined: May 01, 2008
Posts: 43
Hi All,

I have 5 text fields in a HTML form. A maxlength value is given on three of them. In javascript method I want to get all the fields for which a maxlength is given. So, in this case the loop should run 3 times.

Thanks a Lot.
[ December 19, 2008: Message edited by: Bear Bibeault ]

Regards
SaurabhSri (SCJP 1.5)
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
The basic idea is this:

Well get all of the form element with getElementByTagName, loop through the result set, check to see if the type is a textbox, see if the maxLength is set.

See if you can figure that out.

Eric
SaurabhSri Sri
Ranch Hand

Joined: May 01, 2008
Posts: 43
Hi Eric,

Thanks for your reply. The thing is, how would I come to know which field is having maxlength defined. If I am using something like below, the loop is running for all textfields -


Any suggetion?
Thanks
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
A simple alert will show you why it fails in IE



Eric
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61084
    
  66

This is the type of thing that a JavaScript library such as jQuery makes very easy for you.

For example: selects all the input element with an explicit maxlength attribute, which you can then operate upon in various ways, including simply getting an array of the elements.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
SaurabhSri Sri
Ranch Hand

Joined: May 01, 2008
Posts: 43
Hi Everyone,

It means we don't have any way to find this.

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61084
    
  66

Originally posted by SaurabhSri Sri:
It means we don't have any way to find this.
If you don't write any code, then no. Otherwise, Eric showed you one way, and I showed you another.
SaurabhSri Sri
Ranch Hand

Joined: May 01, 2008
Posts: 43
Hi Bear,

It is not possible for us to use jQuery just for this problem because, it's a huge project and some internal framwork is already in use.

If I use a simple alert -
alert(form.elements[i].getAttribute("maxlength"));

It is giving me maxlength for all the textfields. For the textfields having maxlength not given, the alert is showing some long
no. 2147483647.

Thanks
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Well I highly doubt you will set a max length greater than 1000 or more so just build a filter. Do not make it more complicated than it needs to be!

Eric
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61084
    
  66

Originally posted by SaurabhSri Sri:
It is not possible for us to use jQuery just for this problem because, it's a huge project and some internal framwork is already in use.
jQuery is written in JavaScript like everything else, so anything it can do, you can do with raw JavaScript (it's just usually better when someone else has already done the work for you).

So, to say it can't be done is nonsense. You'll have to follow Eric's advice.
 
Don't get me started about those stupid light bulbs.
 
subject: Acess text fields for which maxlength is defined