ainoy31 Posted January 4, 2008 Share Posted January 4, 2008 Hello- I have a javascript that auto formats a text field as numbers are entered. So, if you enter 1111223333, then it will be 1111.22.3333. Here is the script I am using: var aChar = new Array(' ', '(', ')', '-', '.'); var maxhtsuslength = 12; var htsus1; var htsus2; var cursorposition1; function ParseForNumber3(object) { htsus1 = ParseChar1(object.value, aChar); } function ParseForNumber4(object) { htsus2 = ParseChar1(object.value, aChar); } function backspacerUP1(object,e) { if(e) { e = e } else { e = window.event } if(e.which) { var keycode = e.which } else { var keycode = e.keyCode } ParseForNumber3(object) if(keycode >= 48) { ValidateNumber(object) } }//end of function backspacerUP(object,e) function backspacerDOWN1(object,e) { if(e) { e = e } else { e = window.event } if(e.which) { var keycode = e.which } else { var keycode = e.keyCode } ParseForNumber4(object) }//end of function backspacerDOWN(object,e) function Getcursorposition1() { var t1 = htsus1; var t2 = htsus2; var bool = false for (i=0; i<t1.length; i++) { if (t1.substring(i,1) != t2.substring(i,1)) { if(!bool) { cursorposition1=i bool=true } } } }//end of function Getcursorposition1() function ValidateNumber(object) { var p = htsus1 p = p.replace(/[^\d]*/gi,"") if (p.length < 4) { object.value=p } else if(p.length==4) { pp=p; d4=p.indexOf('.') if(d4==-1) { pp=pp+"."; } object.value = pp; } else if(p.length>4 && p.length < 7) { p =p + "."; l30=p.length; p30=p.substring(0,4); p30=p30+"." p31=p.substring(4,l30); pp=p30+p31; object.value = pp; } else if(p.length >= 7) { p =p + "."; l30=p.length; p30=p.substring(0,3); p30=p30+"." p31=p.substring(3,l30); pp=p30+p31; l40 = pp.length; p40 = pp.substring(0,7); p40 = p40 + "." p41 = pp.substring(7,l40); ppp = p40 + p41; object.value = ppp.substring(0, maxhtsuslength); } Getcursorposition1() if(cursorposition1 >= 0) { if (cursorposition1 == 0) { cursorposition1 = 3 } else if (cursorposition1 <= 3) { cursorposition1 = cursorposition1 + 1 } else if (cursorposition1 <= 6) { cursorposition1 = cursorposition1 + 2 } else if (cursorposition1 == 6) { cursorposition1 = cursorposition1 + 2 } else if (cursorposition1 == 7) { cursorposition1 = cursorposition1 + 3 e1=object.value.indexOf('.') e2=object.value.indexOf('.') if (e1>-1 && e2>-1) { if (e2-e1 == 4) { cursorposition1 = cursorposition1 - 1 } } } else if (cursorposition1 < 10) { cursorposition1 = cursorposition1 + 3 } else if (cursorposition1 == 10) { cursorposition1 = cursorposition1 + 1 } else if (cursorposition1 >= 11) { cursorposition1 = cursorposition1 } }//end of if(cursorposition1 >= 0) }//end of function ValidatePhone(object) function ParseChar1(sStr, sChar) { if (sChar.length == null) { aChar = new Array(sChar); } else aChar = sChar; for (i=0; i<aChar.length; i++) { sNewStr = ""; var iStart = 0; var iEnd = sStr.indexOf(sChar[i]); while (iEnd != -1) { sNewStr += sStr.substring(iStart, iEnd); iStart = iEnd + 1; iEnd = sStr.indexOf(sChar[i], iStart); } sNewStr += sStr.substring(sStr.lastIndexOf(sChar[i]) + 1, sStr.length); sStr = sNewStr; }//end of for (i=0; i<aChar.length; i++) return sNewStr; }//end of function ParseChar(sStr, sChar) <input type="text" name="htsus" onkeydown="javascript:backspacerDOWN(this,event);" onkeyup="javascript:backspacerUP(this,event);" /> The format comes out as 111.122.3333 but need it to be 1111.22.3333. I am probably missing something stupid since I have been coding too long. Much appreciation. AM Quote Link to comment Share on other sites More sharing options...
jvrothjr Posted January 4, 2008 Share Posted January 4, 2008 substring(0,3); replaced with substring(0,4); looks to be reading 3 char and not 4 Quote Link to comment Share on other sites More sharing options...
ainoy31 Posted January 5, 2008 Author Share Posted January 5, 2008 Yeah. I replaced the 3 with a 4 and that fixed my issue. For some reason I was thinking the count starts from 0.... on up like an array. Thanks. AM Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.