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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a form for SS#

Status
Not open for further replies.

CblSreyes22

Technical User
Jun 30, 2003
6
US
Hi all,
I am new to web development, I would like to create a form that would automatically format the SS# as you type it in (123-12-123). Is that possible?


-CBLSR
 
First of many iterations:

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/strict.dtd">[/URL]

<html>
<head>
<title>new document</title>

<script language="javascript" type="text/javascript">
<!--

function formatSSN(el) {

    var ssn = '';
	var c = '';

	if (el.value.length == 3 || el.value.length == 6) el.value += "-";
	if (el.value.length > 11) el.value = el.value.substring( 0, 11 );

    for ( var i = 0; i < el.value.length; i++ ) {
		c = el.value.charCodeAt( i );
		if (i == 3 || i == 6) {
			ssn += "-";
		} else {
			if ( c >= 48 && c <= 57 )
			  ssn += el.value.charAt( i );
		}
	}
	el.value = ssn;
}

-->
</script>

</head>

<body>

<form name="f">
  <input type="text" name="ssn" onkeyup="return formatSSN(this);" />
</form>

</body>

</html>

*cLFlaVA
----------------------------
[tt]insert funny quotation here.[/tt]
 
I like this better:

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/strict.dtd">[/URL]

<html>
<head>
<title>new document</title>

<script language="javascript" type="text/javascript">
<!--

function formatSSN(el) {

    var ssn = '';
	var c = '';

	// cut any ssn longer than 11 digits (123-56-8901)
	if (el.value.length > 11) el.value = el.value.substring( 0, 11 );

	// store only numbers in a string
    for ( var i = 0; i < el.value.length; i++ ) {
		c = el.value.charCodeAt( i );
		if ( c >= 48 && c <= 57 ) ssn += el.value.charAt( i );
	}

	// build ssn
	if (ssn.length >= 3 && ssn.length < 5)
      ssn = ssn.substring( 0, 3 ) + "-" + ssn.substring( 3, 5 );
	else if (ssn.length >= 5)
      ssn = ssn.substring( 0, 3 ) + "-" + ssn.substring( 3, 5 ) + "-" + ssn.substring( 5, 9 );

	// display formatted ssn
	el.value = ssn;
}

-->
</script>

</head>

<body>

<form name="f">
  <input type="text" name="ssn" onkeyup="return formatSSN(this);" />
</form>

</body>

</html>

*cLFlaVA
----------------------------
[tt]insert funny quotation here.[/tt]
 
Hey cLFlaVa,

Thanks, this is great it does exactly what I am looking for.


-CBLSR
 
Might I suggest using a Regular Expression instead? Much less code:

Code:
<html>
<head>
<script type="text/javascript">
function validSS(x)
{
  var ss = /^([\d]{3})([\d]{2})([\d]{4,})$/.exec(x.value);
  x.value = ss[1]+"-"+ss[2]+"-"+ss[3];

}
</script>
</head>
<body>
<form>
<input type="text"/ onchange="validSS(this)";>
</form>
</body>
</html>



Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
Or multiple textboxes:
Code:
<html>
<head>
<title>SS#</title>
<script type="text/javascript">
<!--

function moveNext(elem)
{
  if (elem.value.length >= elem.maxlength)
  {
    while (elem.nodeType.toUpperCase() != "INPUT" && elem.type.toUpperCase() != "TEXT")
    {
      if (elem.nextSibling)
        elem = elem.nextSibling;
      else if (elem.parentNode)
        elem = elem.parentNode)
      if (!elem || elem.nodeType.toUpperCase() == "FORM" || !elem.parentNode)
      {
        alert("Last element reached!");
        return;
       }
    }
    elem.focus();
  }
}

// -->
</script>
</head>
<body>
<form>
 <input type="text" name="str1" maxlength="3" onkeydown="moveNext(this);">-
 <input type="text" name="str2" maxlength="2" onkeydown="moveNext(this);">-
 <input type="text" name="str3" maxlength="3">
</form>
</body>
</html>
Not tested though...


--Chessbot

There is a level of Hell reserved for probability theorists in which every monkey that types on a typewriter produces a Shakesperean sonnet.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top