PDA

View Full Version : help to change the paramenter from select onchange


eduacsp
04-26-2006, 01:23 PM
hi all,

I have this code, and i want to change automatically the third parameter of the function txtBoxFormat when the user change the <select> for another value, this is possible?

im a javascript newbie :(



<html>
<head>

<script language="JavaScript">
<!-- Begin
function txtBoxFormat(objForm, strField, sMask, evtKeyPress) {
var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;

if(document.all) { // Internet Explorer
nTecla = evtKeyPress.keyCode; }
else if(document.layers) { // Nestcape
nTecla = evtKeyPress.which;
}

sValue = objForm[strField].value;

// Limpa todos os caracteres de formatação que
// já estiverem no campo.
sValue = sValue.toString().replace( "-", "" );
sValue = sValue.toString().replace( "-", "" );
sValue = sValue.toString().replace( ".", "" );
sValue = sValue.toString().replace( ".", "" );
sValue = sValue.toString().replace( "/", "" );
sValue = sValue.toString().replace( "/", "" );
sValue = sValue.toString().replace( "(", "" );
sValue = sValue.toString().replace( "(", "" );
sValue = sValue.toString().replace( ")", "" );
sValue = sValue.toString().replace( ")", "" );
sValue = sValue.toString().replace( " ", "" );
sValue = sValue.toString().replace( " ", "" );
fldLen = sValue.length;
mskLen = sMask.length;

i = 0;
nCount = 0;
sCod = "";
mskLen = fldLen;

while (i <= mskLen) {
bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/"))
bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

if (bolMask) {
sCod += sMask.charAt(i);
mskLen++; }
else {
sCod += sValue.charAt(nCount);
nCount++;
}

i++;
}

objForm[strField].value = sCod;

if (nTecla != 8) { // backspace
if (sMask.charAt(i-1) == "9") { // apenas números...
return ((nTecla > 47) && (nTecla < 58));
} // números de 0 a 9
else { // qualquer caracter...
return true;
}
}else {
return true;
}
}
// End -->
</script>

</head>
<body>

<form method="POST" action="<?=$PHP_SELF;?>" name="Form">

<SELECT onchange="">
<OPTION value="1">1
<OPTION value="2">2
<OPTION value="3">3
</SELECT>
<p>
<input type='text' name='test' value='' maxlength="13" size="14" onkeypress="return txtBoxFormat(document.Form, 'test', '999-9999', event);">

</form>
</body>
</html>

Kor
04-26-2006, 01:32 PM
What your code suppose to do? I guess there might be an easier way on using Regular Expression.

eduacsp
04-26-2006, 01:44 PM
when the user change the <select>, he must change the third parameter on the txtBoxFormat function too.

Kor
04-26-2006, 01:58 PM
No Sir, you have not understood me. I have asked you what the whole code suppose to do. You see, the way it is, the code will work only on IE. Besides all it is an old type of javascript validator, or something like that. Now, What does this code?, as we might build another code for you, simplier, based on other methods (DOM, RegExp)

eduacsp
04-26-2006, 02:15 PM
this code suppose to create a (xxx-xxxx) mask on the <input>, but this mask will change if the user change the value from <select>. Ex:
<select value> = mask parameter
1 = xxx-xxxx
2 = xx/xxxxx
3 = xxx/xxxxx

did you get the problem?

eduacsp
04-26-2006, 08:16 PM
anyone????

Kor
04-27-2006, 09:43 AM
what must be xx ? only number characters? And there must be always exactly 7 digits?

eduacsp
04-27-2006, 12:02 PM
if i put 9...like (9999-99) then the user will only able to type numbers -ex: 8398-33

if i put ! ... like (!!!!-!!) then the user will able to type numbers or chars - ex: 8ki3-3i

Kor
04-27-2006, 01:01 PM
if i put 9...like (9999-99) then the user will only able to type numbers -ex: 8398-33

if i put ! ... like (!!!!-!!) then the user will able to type numbers or chars - ex: 8ki3-3i
put what and where? Please detail.

eduacsp
04-27-2006, 01:09 PM
forget it, i solve this right now

Kor
04-27-2006, 01:22 PM
as you wish. A RegExp based on solution might have been simplier... But if you say it works now, so be it.