4 Replies - 3513 Views - Last Post: 27 April 2011 - 07:44 PM Rate Topic: -----

#1 duerz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 23-February 10

PHP Validate Social Security Number

Posted 27 April 2011 - 03:31 PM

Hello,

I'm trying a simple validation on a social security number.

Could someone please tell me what I am doing wrong?

My html:

!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="en-us" http-equiv="Content-Language" />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Enter SSN</title>
</head>

<body>


<form method="post" action="ssn.php">
<p>Enter SSN:&nbsp; <input name="ssn" style="width: 175px" type="text" /></p>
<p><input name="Button1" type="submit" value="Check SSN" /></p>
</form>


</body>

</html>




and ssn.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php
$ssn = trim($_POST['ssn']);
?>
<head>
<meta content="en-us" http-equiv="Content-Language" />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>

<body>
    <h1>Error</h1>
    <?php
    if (!eregi("^(\d{3})\-?(\d{2})\-?(\d{4})$", $ssn)) {
    echo "<p> That is not a valid social security number!</p>".
    "<p>Please return to the previous page and try again.</p>";
        exit;
        }
    ?>
<p>&nbsp;</p>
<p>&nbsp;</p>

</body>

</html>




Any help would be appreciated

Is This A Good Question/Topic? 0
  • +

Replies To: PHP Validate Social Security Number

#2 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,356
  • Joined: 15-February 11

Re: PHP Validate Social Security Number

Posted 27 April 2011 - 03:46 PM

Use preg_match instead of eregi.

Remember to always use delimiters.
Was This Post Helpful? 0
  • +
  • -

#3 duerz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 23-February 10

Re: PHP Validate Social Security Number

Posted 27 April 2011 - 05:06 PM

Nope that didnt work - I tried !preg_match and preg_match.
Was This Post Helpful? 0
  • +
  • -

#4 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,356
  • Joined: 15-February 11

Re: PHP Validate Social Security Number

Posted 27 April 2011 - 07:08 PM

From your expression your social security number is xxx-xx-xxxx. A ^ and a $ represent the beginning and the end of the string respectively. So you got that part down.

Now for your delimeters...You are urged to use something that you won't have to use in the actual expression. My personal favourite is the # sign. They should be placed at the beginning and ending of the expression.

'#^$#'


Now we're testing for 3 numbers, a -, 2 numbers, a - then 4 numbers.

preg_match('#^(\d{3})-(\d{2})-(\d{4})$#', $ssn, $matches);


You had everything there but you also had a ? which means that the preceding character is optional. So therefore your expression will be matching more than it's suppose to.

Resources
http://www.regular-e...o/optional.html
Was This Post Helpful? 0
  • +
  • -

#5 duerz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 23-February 10

Re: PHP Validate Social Security Number

Posted 27 April 2011 - 07:44 PM

Thank you very much - the delimiter portion was left out of my text book?

Go figure?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1