Jump to content

Recommended Posts

Hello Everyone,

 

I have this code that  I created and I am suck on it. Bascially this code that I am working on is a social securtiy format validation. (111-11-1111) So once they click submit if it is wrong then I want it to say Invalid SS format on top of the form part; but if it is right I want it to display Valid SS format.

 

This is what I have;

<?

$field='111-22-3333';

if (ereg ('^\(?[0-9]{3} [^0-9]{0,2}([0-9]{2}) [^0-9]?([0-9]{4})?$', $field)) 
{
echo 'Valid Social Security Format';
}

else
{
echo 'Invalid Social Security Format';
}

?>

<form method="post" action="test.html" name="number1">

Social Securtiy <input type="text" name="socials" id="socials" />

<input type="submit" value="Submit" name="Submit" />

</form>

 

Thanks,

 

KashMoney

Link to comment
https://forums.phpfreaks.com/topic/80060-solved-social-security-validation/
Share on other sites

using ereg your want this

 

<?php
//valid
//$field='111-22-3333';
//$field='111-2-3333';
//$field='111--3333';// as you have up to two digest

if (eregi('^[[:digit:]]{3}\-[[:digit:]]{0,2}\-[[:digit:]]{4}$', $_POST['socials']))
{
echo 'Valid Social Security Format';
}else{
echo 'Invalid Social Security Format';
}

?>

<form method="post" action="test.html" name="number1">

Social Securtiy <input type="text" name="socials" id="socials" />

<input type="submit" value="Submit" name="Submit" />

</form>
?>

Alright i have added in some error checking to debug and make sure this works.

What i do first is check to see if the form is sent and if not show the form,

then i validate it using ereg like you did but using a little less complicated ereg.

Not to worry this ereg validates it just as you were.

Then if it is validated it tells the user that it is and if not it tells the user that it is not.

If some of the information was not filled out we tell them this and show the form with what they posted

If the form was submitted correctly and $message is set we will show the user the message, if not the script stops and tells the user the error

 

<?php
if($_POST['submit'] && !(isset($message) || isset($error))) {
	//if the form is submitted check to see if all the data was filled out
	if($_POST['social']) {
		//if all og the data is filled out check to see if it is valid
		//format is xxx-xx-xxxx
		$social = $_POST['social'];
		if(ereg("([0-9]{3})-([0-9]{2})-([0-9]{4})", $social) { 
			//if it is valid return that it is
			$message = 'Valid Social Security Format';
		}
		else {
			//if it is not valid tell the user
			$error = 'Invalid Social Security Format';
		}
	}
	else {

		$error = 'You must fill out all of the data';
	}
}
elseif($_POST['submit'] && (isset($message) || isset($error))) {
	if(isset($message)) {
		print $message;
	}
	elseif(isset($error)) {
		print $error . '<br />';
		print 'NOTE: Social secrity format is xxx-xx-xxxx';
		print '<form action=' . $_SERVER['PHP_SELF'] . ' method=post>';
		print '<label for=social>Social Security #: </label><input type=text name=social id=social value="' . $_POST['social'] . '" /><br />';
		print '<input type=submit name=submit value=Submit />';
		print '</form>';
	}
	else {
		print 'An unknown error occured. Please notify the site admin.';
	}
}
elseif(!$_POST['submit'] && !(isset($message) || isset($error))) {
	print 'NOTE: Social secrity format is xxx-xx-xxxx';
	print '<form action=' . $_SERVER['PHP_SELF'] . ' method=post>';
	print '<label for=social>Social Security #: </label><input type=text name=social id=social /><br />';
	print '<input type=submit name=submit value=Submit />';
	print '</form>';
}
else {
	print 'An unknown error occured. Please notify the site admin.';
}
?>

I put int the code that you did and I got an error Parse error: syntax error, unexpected '{' in /home/mmaaaco/public_html/kkashou/homework/quiz.html on line 16

 

This is my code with your code;

 

<!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 http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>

<?php
if($_POST['submit'] && !(isset($message) || isset($error))) {
	//if the form is submitted check to see if all the data was filled out
	if($_POST['social']) {
		//if all og the data is filled out check to see if it is valid
		//format is xxx-xx-xxxx
		$social = $_POST['social'];
		if(ereg("([0-9]{3})-([0-9]{2})-([0-9]{4})", $social) { 
			//if it is valid return that it is
			$message = 'Valid Social Security Format';
		}
		else {
			//if it is not valid tell the user
			$error = 'Invalid Social Security Format';
		}
	}
	else {

		$error = 'You must fill out all of the data';
	}
}
elseif($_POST['submit'] && (isset($message) || isset($error))) {
	if(isset($message)) {
		print $message;
	}
	elseif(isset($error)) {
		print $error . '<br />';
		print 'NOTE: Social secrity format is xxx-xx-xxxx';
		print '<form action=' . $_SERVER['PHP_SELF'] . ' method=post>';
		print '<label for=social>Social Security #: </label><input type=text name=social id=social value="' . $_POST['social'] . '" /><br />';
		print '<input type=submit name=submit value=Submit />';
		print '</form>';
	}
	else {
		print 'An unknown error occured. Please notify the site admin.';
	}
}
elseif(!$_POST['submit'] && !(isset($message) || isset($error))) {
	print 'NOTE: Social secrity format is xxx-xx-xxxx';
	print '<form action=' . $_SERVER['PHP_SELF'] . ' method=post>';
	print '<label for=social>Social Security #: </label><input type=text name=social id=social /><br />';
	print '<input type=submit name=submit value=Submit />';
	print '</form>';
}
else {
	print 'An unknown error occured. Please notify the site admin.';
}
?>

<form method="post" action="$PHP_SELF" >

Social Securtiy <input type="text" name="social" id="social" />

<input type="submit" value="Submit" name="Submit" />

</form>


</body>
</html>

 

The code that i submitted had the form in it so you do not need it after my code. If you read what i wrote you would know this.  teng84's solution is correct you need another ) after the ereg statement

 

elseif(!$_POST['submit'] && !(isset($message) || isset($error))) {

 

and in that line what is the function of "!"  !(isset($message)  ???

 

!(isset($message) || isset($error))

 

checks to make sure that both $message and $error are not set. it is the same as saying

 

(!isset($message) || !isset($error))

For some reason they are not working. But I am going for this code here;

<?

if (eregi('^[[:digit:]]{3}\-[[:digit:]]{0,2}\-[[:digit:]]{4}$', $_POST['socials']))
{
echo 'Valid Social Security Format';
}

else
{
echo 'Invalid Social Security Format';
}

?>

<p>NOTE: Social secrity format is xxx-xx-xxxx</p>

<form method="post" action="quiz.html" name="q1">

Social Securtiy <input type="text" name="socials" id="socials" />

<input type="submit" value="Submit" name="Submit" />

</form>

 

Its just when i go to the page it already has the message "Invalid Social Security Format" I do not want that message to show when someone load on to the page! How can I fix that part?

 

Thanks,

 

KashMoney

 

Change your code to this

 

<?php

if (isset($_POST['Submit'])){

if (eregi('^[[:digit:]]{3}\-[[:digit:]]{0,2}\-[[:digit:]]{4}$', $_POST['socials']))
{
echo 'Valid Social Security Format';
}

else
{
echo 'Invalid Social Security Format';
}
}

?>

<p>NOTE: Social secrity format is xxx-xx-xxxx</p>

<form method="post" action="quiz.html" name="q1">

Social Securtiy <input type="text" name="socials" id="socials" />

<input type="submit" value="Submit" name="Submit" />

</form>

The code that i submitted had the form in it so you do not need it after my code. If you read what i wrote you would know this.  teng84's solution is correct you need another ) after the ereg statement

 

elseif(!$_POST['submit'] && !(isset($message) || isset($error))) {

 

and in that line what is the function of "!"  !(isset($message)  ???

 

!(isset($message) || isset($error))

 

checks to make sure that both $message and $error are not set. it is the same as saying

 

(!isset($message) || !isset($error))

 

i havent tried that but i believe your code works this way

 

!(isset($message) || isset($error))  is logically  equal to !(true)||(true)  so whats gonna be the result hmm i cant figure out but i guess that completely logically wrong and i guess it wont work too

 

 

 

 

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.