×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Hi I have a nice copy field script

Hi I have a nice copy field script

Hi I have a nice copy field script

(OP)
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 (http://www.tek-tips.com/viewthread.cfm?qid=437438) 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="http://binaryoptionsindicatorsignals.com">Top Binary Options Signals System</a>

RE: Hi I have a nice copy field script

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

RE: Hi I have a nice copy field script

(OP)
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: http://binaryoptionsindicatorsignals

RE: Hi I have a nice copy field script

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.
Foo              Bar
1234567890    1234-567-890
 

----------------------------------
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close