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

How can I insert the dash marks into a SS# passed into a variable? 1

Status
Not open for further replies.

Apollo6

Technical User
Jan 27, 2000
418
US
I have an opening page where the user puts in their Social Security number. Lets say that they put it in as 123456789, no dashes. I then pass it to the next page, put it into a variable and use it in a query.

1. I need it to have the dashes, 123-45-6789. How can I insert the dashes? Is there a function that I can use to do this?

2. I would think the best way would be to have some type of check that looks to see if they have put it in with or without dashes. If they have, use what they put in, else put the dashes in. What function can I use to look for dashes in a string?

Thanks for any suggestions.
 
I would strip out any non-digit characters, then attempt to add the dashes where they belong, then test the string to see if if matches the format I need.

Something like this:
Code:
<?php
$ssn = '123456789';

$foo = $ssn;
$foo = preg_replace ('/[^\d]/', '', $foo);
$foo = preg_replace ('/^(\d{3})(\d{2})(\d{4})$/', '$1-$2-$3', $foo);

$good_ssn = FALSE;
if (preg_match ('/^\d{3}-\d{2}-\d{4}$/', $foo))
{
	$good_ssn = TRUE;
	$ssn = $foo;
}

if ($good_ssn)
{
	print &quot;good:&quot;;
}
else
{
	print &quot;bad:&quot;;
}

print $ssn;

?>

This script makes heavy use of PHP's preg_replace() function ( Want the best answers? Ask the best questions: TANSTAAFL!
 
Works as advertised... Thanks for the suggestion.

The following line is used to strip out any non-numeric characters:
$foo = preg_replace ('/[^\d]/', '', $foo);

What I am researching now is how to trap the case where the user put in an alpha character in the social security number. I want to display a message box making them aware of their error and resetting the input box.

In Visual Basic, Msgbox() is provided for this action, then clearing the input box and resetting the focus. Does PHP provide a similar function?
 
PHP runs server side, after the submit... so no, not really... you'd have to rewrite the page, along with the MsgBox or Alert as part of your HTML code.

The best way to do that would be to use a client side script in the page to validate the information before it even gets to PHP. I'd suggest trying the javascript forum, vbscript would work too, but I can't in good consience advise that.

-Rob
 
PHP runs on the server side. To create a message box on the client side, you would have to use some sort of client side scripting, like JavaScript. //Daniel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top