PDA

View Full Version : text box edit code:..helpppppp


vijk2001
09-11-2002, 08:02 PM
I have the following script:.
(given to me on this site..)
My requirement is : I have two Text edit fields...
If LinkName >=4 chars then Linkcode should automatically be the first 4 chars.
If LinkName < 4 chars then LinkCode should be LinkName PLUS appended O after it.
So i want LinkCode to be automatically ,dynamically populated
But in below code nothing happens..I click Submit and nothing ..why.....btw...txt shd be LinkCode.

<script>
function chk_len(what) {
txt = what.LinkName.value;
if (txt.length>4) {
txt = txt.subString(0,3);
}else{
while (txt.length<4) {
txt += "0";
}
}
return true;
}
</script>


Vij

<form onSubmit="chk_len(this);">

RadarBob
09-11-2002, 09:08 PM
try "substring()" in all lower case. Javascript is case sensitive.

Also, what if txt is 1 or 2 characters? It kinda looks like you want exactly 4 characters, no?

Ökii
09-11-2002, 10:45 PM
function chk_len(formname) {
arg = document.forms[formname].elements['linkname'].value;
for(adb=0;adb<(4-arg.length);adb++) {
arg+="0";
}
document.forms[formname].elements['linkname'].value = arg;
}

note: 'linkname' would be in quotes as it is not passed as a variable into the function. The prob you had with yours was that your re-interpreted value of txt was not being reassigned to the form element at the end of your script.
I used a for.....loop that runs from 0 until it is the same or greater than 4 minus the length of txt (or arg in my case) as it seemed the simplest way of adding zeros to the field.

Hope that helps anyway.

vijk2001
09-12-2002, 03:33 PM
So do i replace my whole function with yours or do i add the for loop functionality?
Thanks..
vij