KashMoney Posted December 4, 2007 Share Posted December 4, 2007 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 Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 4, 2007 Share Posted December 4, 2007 You never really stated the problem, but doing a search on google this is the regex I came up with ^\d{3}-\d{2}-\d{4}$ You may want to give that a try. Quote Link to comment Share on other sites More sharing options...
MadTechie Posted December 4, 2007 Share Posted December 4, 2007 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> ?> Quote Link to comment Share on other sites More sharing options...
mr_mind Posted December 4, 2007 Share Posted December 4, 2007 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.'; } ?> Quote Link to comment Share on other sites More sharing options...
KashMoney Posted December 4, 2007 Author Share Posted December 4, 2007 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> Quote Link to comment Share on other sites More sharing options...
teng84 Posted December 4, 2007 Share Posted December 4, 2007 you missed ) in this line if(ereg("([0-9]{3})-([0-9]{2})-([0-9]{4})", $social) { should be if(ereg("([0-9]{3})-([0-9]{2})-([0-9]{4})", $social)) { Quote Link to comment Share on other sites More sharing options...
teng84 Posted December 4, 2007 Share Posted December 4, 2007 elseif(!$_POST['submit'] && !(isset($message) || isset($error))) { and in that line what is the function of "!" !(isset($message) ??? Quote Link to comment Share on other sites More sharing options...
mr_mind Posted December 4, 2007 Share Posted December 4, 2007 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)) Quote Link to comment Share on other sites More sharing options...
KashMoney Posted December 4, 2007 Author Share Posted December 4, 2007 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 Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted December 4, 2007 Share Posted December 4, 2007 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> Quote Link to comment Share on other sites More sharing options...
teng84 Posted December 4, 2007 Share Posted December 4, 2007 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 Quote Link to comment Share on other sites More sharing options...
KashMoney Posted December 4, 2007 Author Share Posted December 4, 2007 Thank you very much everyone for all of your help! KashMoney Quote Link to comment Share on other sites More sharing options...
mr_mind Posted December 4, 2007 Share Posted December 4, 2007 Wow what is with the flame? for those who dont know what this if statement elseif(!$_POST['submit'] && !(isset($message) || isset($error))) { if nothing is posted and the error nor message is set then continue. There is no flaw in that logic Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.