Jump to content

[SOLVED] Auto Format input field


ainoy31

Recommended Posts

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.