PDA

View Full Version : expend the text input


frontline
11-19-2002, 11:40 AM
is there any way to expend the text input html and to add a misc property
like checking max length so i could check it like :
MyTextInput.CheckLen() or something .. like in every text input in my page.

chrismiceli
11-19-2002, 01:52 PM
you can check the maxlength of a input text if you gave it one by doing this.
alert(document.forms[0].elements[0].maxlength);
you can check the lenght of it like this
alert(document.forms[0].elements[0].length);

frontline
11-19-2002, 02:11 PM
ok sorry if i was misunderstood , what i meant was in theory simple
i was wandering if i can create new property to html build in object like the "input text box" the maxLen was just example
it can be simple MyText.foo property that will hold 1 or 0 or what ever somekind of custom text box input with my made properties and methods
hope you got my point now
thanks

glenngv
11-20-2002, 05:36 AM
you can do that in DOM-compliant browsers.

<input type="text" name="myText" value="test" myprop="blah" onfocus="alert('current value:\n'+this.myprop);this.myprop+=',blah';alert('new value:\n'+this.myprop);this.blur()">


generally, you would access it like this:

document.FormNameHere.myText.myprop

beetle
11-20-2002, 07:58 AM
What glenn said except for

document.FormNameHere.myText.myprop

Typically, as I've read, HTMLElement attributes aren't exposed as object properties unless the DOM recognizes them. So, we need the getAttribute() method

document.FormNameHere.myText.getAttribute('myprop');

glenngv
11-20-2002, 08:19 AM
thanks beetle for the correction

RadarBob
11-20-2002, 02:57 PM
Do it with good ol' objects....

Here is a text object that has a max length attribute.

With this example you can make objects of any length.

You could make differently named objects each with it's own built-in limit.

You could also go crazy with things like excluding (or limiting to) certain characters for that particular text object type.

Don't forget you also inherit all the text tag attributes.


function textObject (textField, maxLen) {
this.limit = maxLen;
this.text = textField;

//methods
this.inLimits = inLimits;
this.print = printTextObj
}// textObject

function inLimits () {
var justRight = true;

if (this.text.length > this.limit) {
justRight = false;
}

return justRight;
}// inLimits()


function printTextObj () {
alert (this.text.value);
}

. . .

var titleLength = 100;
var DescriptLength = 255;
var shortTitleLength = 25;

var myText = new textObject (document.myformname.textfieldname, titleLength);

if (myText.inLimits()) {
alert = ("Not too long");
}else{
alert ("too long");
}

myText.print();


Keep in mind there is also a maxlength attribute for text tags that will not allow input greater than that limit.