View Full Version : Javascript "-"
Thanotos
01-12-2006, 09:45 AM
Ohh the agony.
I have been struggling with trying to code javascript within a textarea. I am wanting to get " - "'s autopopulate when I enter in a telephone number.
Essentially I have played with running a loop that after entering three numbers would insert a ' - ' and then after three more numbers would enter another ' - '.
Any one have suggestions or ideas?
coothead
01-12-2006, 12:31 PM
Hi there Thanotos,
and a warm welcome to these forums. :loopy:
I hope that this little script will alleviate your agony. :supereek:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>insert a hyphen</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
//<![CDATA[
var c=0;
function insertHyphen() {
df=document.forms[0][0];
temp=df.value.charAt(c);
if((isNaN(temp))&&(temp!='-')){
df.value=df.value.replace(temp,'');
}
else {
c++;
}
if(df.value.length%4==3) {
df.value=df.value+'-';
c++;
if(navigator.userAgent.indexOf("Opera")!=-1){
versionindex=navigator.userAgent.indexOf("Opera")+6;
if (parseInt(navigator.userAgent.charAt(versionindex))>=8) {
df.blur();
}
}
}
}
//]]>
</script>
</head>
<body onload="document.forms[0][0].focus()">
<form action="#">
<div>
<input type="text" onkeyup="insertHyphen()"/>
</div>
</form>
</body>
</html>
Unfortunately Opera, for some strange reason places the cursor before the hyphen so I have added the
highlighted code to blur the input box. If I find a better method I will come back with it.
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.