Hi,
I have a text box. Actually say I have two text boxes txt1 and txt2, both are phone number fields. The one I enter and doesn't have any auto formatting associated. The second text box txt2 has autoformatting of the dates on key press event. Now everything seems to be working okay, it formats okay, doesn't allow more than 10 digits and refomats in case the user changes couple of digits etc.. The cursor position is also fine. earlier it always went to the end of the text anytime there is a key down or up event, now all that is fixed. The only strange problem I am facing now is
Incase I try to edit say 3 continous digits in the text, using selecting the text at a time and then inserting the new three numbers, it replaces the whole text instead, but for txt1, it works fine, as there is no keyevent on the text box.
Am I clear ?
Say for eg:- I enter 1234567890 in txt1
in txt2 as I enter 1234567890 it gets formatted to (123) 456-7890.
Now I need to change 456 with 654, okay so I select 456 and I enter 654, then in txt1 it becomes 1236547890, as expected, but txt2 the whole text is replaced with 654, I donot understand why it doesn't change only the selected text portion ?
Something I noted is in case I select 456 and press delete and then enter 654, it works okay, the end result is (123) 654-7890.
Can somebody help please ?
Here is my sample code.
<input type="text" maxlength="14" name='FAC_FAX' onkeypress="format_myphone(this.value, this)"/>
Javascript
function format_myphone(myval, myobj) {
var FmtStr="";
var val = "";
var flag1 = "";
var flag2 = "";
var flag3 = "";
var nochange = 'N' ;
e = window.event;
var keyChar = String.fromCharCode(e.keyCode);
var kCode = e.keyCode;
// alert('amicoming here');
if( ( kCode >= 48 && kCode <= 57 )
|| ( kCode >= 65 && kCode <= 90 )
|| ( kCode >= 97 && kCode <= 122 ) )
{ var i=2 ; }
else { return(true); }
val = myval.replace(/\D/g,""
if(myval.charAt(0) == '(')
{ flag1= 1 ;
if ( val.length == 1 || val.length == 0 ) {
FmtStr = myval ;
nochange = 'Y' ;
// return(true);
}
} else flag1 = 0 ;
// alert(flag1) ;
if(myval.charAt(4) == ')')
{ flag2 = 1 ;
if (val.length == 3 ) {
FmtStr = myval ;
nochange = 'Y' ;
// return(true);
}
} else flag2 = 0 ;
// alert(flag2) ;
if(myval.charAt(9) == '-')
{ flag3 = 1 ;
if(val.length == 6 ) {
FmtStr = myval ;
nochange = 'Y' ;
//return(true);
}
} else flag3 = 0 ;
// alert(flag3) ;
if(nochange) {
// alert('anything to change ?') ;
if(val != '' ){
if(flag1==0 && myval.length >= 1 )
{ FmtStr = '('+ myval ;
//alert('why is it not coming here '+myval+'-'+FmtStr);
}
else if(flag2 ==0 && myval.length == 4 && val.length == 3 )
{ FmtStr = myval + ') ' ;
//alert('why is it not coming here 2'+myval+'-'+FmtStr);
}
else if(flag3 ==0 && myval.length == 9 && val.length == 6 )
{ FmtStr = myval + '-' ;
//alert('why is it not coming here 3'+myval+'-'+FmtStr);
}
else if (val.length>=10)
{ FmtStr = format_phone_number1(val) ;
//format_phone_number(FmtStr) ;
}
else { return(false) ; }
} else { FmtStr = myval ; }
} else { FmtStr = myval ; }
myobj.value = FmtStr ;
}
function format_phone_number1(myval) {
var val = "";
if(myval != '' )
val = "("+myval.substr(0,3)+" "+myval.substr(3,3)+"-"+myval.substr(6,4);
return val ;
}
***************************
Please help !!
Thanks
Shanthi
I have a text box. Actually say I have two text boxes txt1 and txt2, both are phone number fields. The one I enter and doesn't have any auto formatting associated. The second text box txt2 has autoformatting of the dates on key press event. Now everything seems to be working okay, it formats okay, doesn't allow more than 10 digits and refomats in case the user changes couple of digits etc.. The cursor position is also fine. earlier it always went to the end of the text anytime there is a key down or up event, now all that is fixed. The only strange problem I am facing now is
Incase I try to edit say 3 continous digits in the text, using selecting the text at a time and then inserting the new three numbers, it replaces the whole text instead, but for txt1, it works fine, as there is no keyevent on the text box.
Am I clear ?
Say for eg:- I enter 1234567890 in txt1
in txt2 as I enter 1234567890 it gets formatted to (123) 456-7890.
Now I need to change 456 with 654, okay so I select 456 and I enter 654, then in txt1 it becomes 1236547890, as expected, but txt2 the whole text is replaced with 654, I donot understand why it doesn't change only the selected text portion ?
Something I noted is in case I select 456 and press delete and then enter 654, it works okay, the end result is (123) 654-7890.
Can somebody help please ?
Here is my sample code.
<input type="text" maxlength="14" name='FAC_FAX' onkeypress="format_myphone(this.value, this)"/>
Javascript
function format_myphone(myval, myobj) {
var FmtStr="";
var val = "";
var flag1 = "";
var flag2 = "";
var flag3 = "";
var nochange = 'N' ;
e = window.event;
var keyChar = String.fromCharCode(e.keyCode);
var kCode = e.keyCode;
// alert('amicoming here');
if( ( kCode >= 48 && kCode <= 57 )
|| ( kCode >= 65 && kCode <= 90 )
|| ( kCode >= 97 && kCode <= 122 ) )
{ var i=2 ; }
else { return(true); }
val = myval.replace(/\D/g,""
if(myval.charAt(0) == '(')
{ flag1= 1 ;
if ( val.length == 1 || val.length == 0 ) {
FmtStr = myval ;
nochange = 'Y' ;
// return(true);
}
} else flag1 = 0 ;
// alert(flag1) ;
if(myval.charAt(4) == ')')
{ flag2 = 1 ;
if (val.length == 3 ) {
FmtStr = myval ;
nochange = 'Y' ;
// return(true);
}
} else flag2 = 0 ;
// alert(flag2) ;
if(myval.charAt(9) == '-')
{ flag3 = 1 ;
if(val.length == 6 ) {
FmtStr = myval ;
nochange = 'Y' ;
//return(true);
}
} else flag3 = 0 ;
// alert(flag3) ;
if(nochange) {
// alert('anything to change ?') ;
if(val != '' ){
if(flag1==0 && myval.length >= 1 )
{ FmtStr = '('+ myval ;
//alert('why is it not coming here '+myval+'-'+FmtStr);
}
else if(flag2 ==0 && myval.length == 4 && val.length == 3 )
{ FmtStr = myval + ') ' ;
//alert('why is it not coming here 2'+myval+'-'+FmtStr);
}
else if(flag3 ==0 && myval.length == 9 && val.length == 6 )
{ FmtStr = myval + '-' ;
//alert('why is it not coming here 3'+myval+'-'+FmtStr);
}
else if (val.length>=10)
{ FmtStr = format_phone_number1(val) ;
//format_phone_number(FmtStr) ;
}
else { return(false) ; }
} else { FmtStr = myval ; }
} else { FmtStr = myval ; }
myobj.value = FmtStr ;
}
function format_phone_number1(myval) {
var val = "";
if(myval != '' )
val = "("+myval.substr(0,3)+" "+myval.substr(3,3)+"-"+myval.substr(6,4);
return val ;
}
***************************
Please help !!
Thanks
Shanthi