Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi I have a nice copy field script

Status
Not open for further replies.

johnwboyd

Programmer
Feb 2, 2018
2
0
0
PH
Hi I have a nice copy field script but I would like the copied phone number to have dashes in it so it reads better:

Here's the relevant code below. Can someone please tell me how to integrate adding dashes to the phone number field ONLY (NOT the entered field)?
I saw here ( a post how to make the dashes appear upon blur but it should be upon entry NOT blur into the copied field.

<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$(function(){
var $foo = $('#foo');
var $bar = $('#bar');
function onChange() {
$bar.val($foo.val());
};
$('#foo')
.change(onChange)
.keyup(onChange);
});
});//]]>

</script>
<input id="foo" name="Phone1" size="10" maxlength="10" />

<input id="bar" style="background:none; border:none; color:#FFFFFF; font-weight:bold; font-size:13px; width:90px" size="10">

<a href=" Binary Options Signals System</a>
 
Not the most optimized, but it should work for what you want:

Code:
function addMask(inputValue)
{

	var mask="(###)##-##-###";
	var retVal = mask;
	
	for(var i=0;i<=inputValue.length-1;i++)
	{
		
		retVal = retVal.replace("#",inputValue.substring(i,i+1));
	}
	
	return retVal;
}

You can define the mask as you need, just make sure you have the # (hashtags) where you want numbers from the entered number to appear. Everything else that is not a hashtag will show up directly. ie. ###something##else##stuff## would end up as: 234something55else22stuff43 for instance.

Then just call it in your onChange event:

Code:
$bar.val(addMask($foo.val));

----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
Hi thank you but I tried and it didn't work. Maybe something is off can you please check:

<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$(function(){
var $foo = $('#foo');
var $bar = $('#bar');
function onChange() {
$bar.val(addMask($foo.val));
};
$('#foo')
.change(onChange)
.keyup(onChange);
});
});//]]>

</script>

<script type='text/javascript'>
function addMask(inputValue)
{

var mask="###-###-####";
var retVal = mask;

for(var i=0;i<=inputValue.length-1;i++)
{

retVal = retVal.replace("#",inputValue.substring(i,i+1));
}

return retVal;
} </script>

And the fields stay the same right?:

<input id="foo">
<input id="bar">

Top Binary Options Signals System:
 
Try it in the same <script> tags:
Code:
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$(function(){
var $foo = $('#foo');
var $bar = $('#bar');
function onChange() {

$bar.val(addMask($foo.val())); 
};
$('#foo')
.change(onChange)
.keyup(onChange);
});
});//]]> 

function addMask(inputValue)
{

var mask="###-###-####";

var retVal = mask;

for(var i=0;i<=inputValue.length-1;i++)
{

retVal = retVal.replace("#",inputValue.substring(i,i+1));
}

return retVal.replace(/#/g,"");
} </script>



<input type="text" id="foo">
<input type="text" id="bar">

The #bar input will get the masked result. the #foo input will remain just as it is typed.
[pre]
Foo Bar
1234567890 1234-567-890
[/pre]

----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top