miniu Posted February 15, 2010 Share Posted February 15, 2010 Hello I have a question. In this script below I want to make some validations, but I don't know how. The validation I want to make is: "Zip code" - this field should validate that its a minimum of 5 digits/numbers and a maximum of 5 digits with NO letters or special characters. If it fails then prompt user to enter information again. <?php include("connect.php"); if($_POST["sub1"]==1) { $fname = addslashes($_POST["txtfname"]); $mname = addslashes($_POST["txtmname"]); $lname = addslashes($_POST["txtlname"]); $username = addslashes($_POST["txtusername"]); $pwd = addslashes($_POST["txtpwd"]); $email = addslashes($_POST["txtemail"]); $email2 = addslashes($_POST["txtemail2"]); $email3 = addslashes($_POST["txtemail3"]); $date_of_birth = addslashes($_POST["txtdate_of_birth"]); $kgdate=explode('-',$date_of_birth); $gdate=$kgdate[2].'-'.$kgdate[0].'-'.$kgdate[1]; $ssn = addslashes($_POST["txtssn"]); $add1 = addslashes($_POST["txtadd1"]); $add2 = addslashes($_POST["txtadd2"]); $city = addslashes($_POST["txtcity"]); $state = addslashes($_POST["txtstate"]); $zip = addslashes($_POST["txtzip"]); $phone1 = addslashes($_POST["txtphone1"]); $phone2 = addslashes($_POST["txtphone2"]); $sel="select username from referees where username='".$username."'"; $res=mysql_query($sel); if(mysql_num_rows($res) > 0) { $msg=1; } else { $que="insert into referees set fname='$fname',mname='$mname',lname='$lname',username='$username',pwd='$pwd',email='$email',email2='$email2',email3='$email3',date_of_birth='$gdate',ssn='$ssn',add1='$add1',add2='$add2',city='$city',state='$state',zip='$zip',phone1='$phone1',phone2='$phone2'"; $run=mysql_query($que); echo mysql_error(); $sql_get="select fname,lname from assigners where username='".$_SESSION["UsErOfAdMiN"]."'"; $res_get=mysql_query($sql_get); $row_get=mysql_fetch_object($res_get); $flname=$row_get->fname." ".$row_get->lname; $mailcontent="<html><head></head><body> <table width='80%' border='0' align='center' cellpadding='0' cellspacing='0'> <tr><td colspan='2'> </td></tr> <tr> <td width='47%' height='56'>Dear <b>$fname $lname</b>,</td> <td width='117' align='center' nowrap='nowrap'></td> </tr> <tr><td colspan='2'>Your Referee account has been created</td></tr> <tr><td colspan='2'> </td></tr> <tr><td colspan='2'>You can login at following link using username and password given below:</td></tr> <tr><td colspan='2'> </td></tr> <tr><td colspan='2'><a href='$sitepath' target='_blank'>$sitepath</a></td></tr> <tr><td colspan='2'> </td></tr> <tr><td colspan='2' align='left'>Username: $username</td></tr> <tr><td colspan='2'> </td></tr> <tr><td colspan='2' align='left'>Password: $pwd</td></tr> <tr><td colspan='2'> </td></tr> <tr><td colspan='2'>With the kindest regards,</td></tr> <tr><td colspan='2'> </td></tr> <tr><td colspan='2'><b>NJSA<br><a href='$sitepath' target='_blank'>$sitepath</a></b></td></tr> </table> </body> </html>"; $to=$email; $subject="Your Referee account has been created"; $from="no-reply@XXX"; // echo $mailcontent; exit; @SendHTMLMail1($to,$subject,$mailcontent,$from); $msg=2; $fname = ''; $mname = ''; $lname = ''; $username = ''; $pwd = ''; $email = ''; $email2 = ''; $email3 = ''; $gdate=''; $ssn = ''; $add1 = ''; $add2 = ''; $city = ''; $state = ''; $zip = ''; $phone1 = ''; $phone2 = ''; } } ?> <HTML> <HEAD><title><?=Site_Title; ?></title> <META http-equiv=Content-Type content="text/html; charset=utf-8"> <SCRIPT language=JavaScript> function Process() { document.all.loading.style.visibility = 'visible'; } function kyb_check() { if(document.getElementById("txtfname").value=="") { alert("Please Enter First Name"); document.getElementById("txtfname").focus(); return false; } if(document.getElementById("txtlname").value=="") { alert("Please Enter Last Name"); document.getElementById("txtlname").focus(); return false; } if(document.getElementById("txtusername").value=="") { alert("Please Enter Username"); document.getElementById("txtusername").focus(); return false; } if(document.getElementById("txtpwd").value=="") { alert("Please Enter Password"); document.getElementById("txtpwd").focus(); return false; } if(document.getElementById("txtemail").value=="") { alert("Please Enter Email"); document.getElementById("txtemail").focus(); return false; } if(!emailInvalid(document.getElementById("txtemail").value)) { alert("Please type valid Email Address"); document.getElementById("txtemail").focus(); return false; } if(document.getElementById("txtemail2").value!="") { if(!emailInvalid(document.getElementById("txtemail2").value)) { alert("Please type valid Email Address"); document.getElementById("txtemail2").focus(); return false; } } if(document.getElementById("txtemail3").value!="") { if(!emailInvalid(document.getElementById("txtemail3").value)) { alert("Please type valid Email Address"); document.getElementById("txtemail3").focus(); return false; } } if(document.getElementById("txtdate_of_birth").value=="") { alert("Please Select Date Of Birth"); document.getElementById("txtdate_of_birth").focus(); return false; } if(document.getElementById("txtadd1").value=="") { alert("Please Enter Address 1"); document.getElementById("txtadd1").focus(); return false; } if(document.getElementById("txtcity").value=="") { alert("Please Enter City"); document.getElementById("txtcity").focus(); return false; } if(document.getElementById("txtstate").value=="") { alert("Please Enter State"); document.getElementById("txtstate").focus(); return false; } if(document.getElementById("txtzip").value=="") { alert("Please Enter Zip"); document.getElementById("txtzip").focus(); return false; } if(document.getElementById("txtphone1").value=="") { alert("Please Enter Phone 1"); document.getElementById("txtphone1").focus(); return false; } } function emailInvalid(s) { if(!(s.match(/^[\w]+([_|\.-][\w]{1,})*@[\w]{2,}([_|\.-][\w]{1,})*\.([a-z]{2,4})$/i) )) { return false; } else { return true; } } </SCRIPT> <script src="calendar.js"></script> <META content="MSHTML 6.00.2600.0" name=GENERATOR> <link href="main.css" rel="stylesheet" type="text/css"> </HEAD> <BODY bgColor=#ffffff leftMargin=0 topMargin=0 marginheight="0" marginwidth="0"> <TABLE cellSpacing=0 cellPadding=5 width="100%" border=0> <TBODY> <TR> <TD height=60 bgcolor="#154671"><img src="images/banner_top.jpg" width="510" height="60"></TD> </TR> <TR> <TD height=20 bgcolor="#1c6995"></TD> </TR> <TR bgColor=#eeeeee> <TD height=15 bgcolor="FFFFFF"></TD> </TR></TBODY></TABLE> <FORM id="frmlogin" name="frmlogin" method="post"> <div id=loading> <table border=1 cellpadding=12 cellspacing=0 bordercolor=#F7D7B7 bgcolor="#FBF5F4"> <tr> <td width="314" align=middle noWrap><font size="2">One moment please. <b>Loading Page..........</b></font></td> </tr> </table> </div> <DIV id=mainbody><!--body--> <TABLE width=650 border=0 align=center cellPadding=0 cellSpacing=15> <TBODY> <TR> <TD noWrap align="left" colSpan=2> <?php if($msg==1) { ?> <table width="100%" border="1" align="left" cellpadding="2" cellspacing="0" bordercolor="#E3D7B7" bgcolor="#F8F5ED"> <tr valign="middle"> <td valign="middle" > <table border="0" cellpadding="0" cellspacing="0"> <tr> <td width="17"><img src="images/ALERT.gif" width="16" height="16"></td> <td align="center" width="500"><font size="2" face="Verdana, Arial, Helvetica, sans-serif" color="#FF0000"><strong> Username Already Exist!!</strong> </font></td> </tr> </table> </td> </tr> </table> <?php } ?> <? if($msg==2) { ?> <table width="100%" border="1" align="left" cellpadding="2" cellspacing="0" bordercolor="#E3D7B7" bgcolor="#F8F5ED"> <tr valign="middle"> <td valign="middle" > <table border="0" cellpadding="0" cellspacing="0"> <tr> <td width="17"><img src="images/ALERT.gif" width="16" height="16"></td> <td align="center" width="500"><font size="2" face="Verdana, Arial, Helvetica, sans-serif" color="green"><strong> Your referee account has been created Successfully.</strong> </font></td> </tr> </table> </td> </tr> </table> <?php } ?> </TD></TR> <TR> <TD vAlign=top width="75%" colspan="2"><!--col002--><!--padding--> <TABLE cellSpacing=0 cellPadding=2 width="100%" bgColor="#1c6995" border=0><TBODY> <TR> <TD> <TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#ffffff border=0> <TBODY> <TR> <TD> <table width="100%" border="0" align="left" cellpadding="3" cellspacing="3" bgColor="#1c6995"> <tr><td colspan="2"> </td></tr> <TR> <TD width="30%"></TD><td><a href="index.php"><font color="#FFFFFF">Click Here to Go Back</font></a></td></TR> <tr> <td colspan="2" align="right" class="menu-a"><span class="a-l"><font color="#FF0000">*</font> indicates required field</span> </td> </tr> <TR> <TD width="30%"></TD><td><strong><font color="#FFFFFF">Create Referee Account</font></strong></td></TR> <TR> <tr> <td width="37%" align="right"><font color="red">*</font> <font color="#FFFFFF">First Name:</font> </td> <td width="63%"><font color="<?=$col?>"> <input type="text" name="txtfname" id="txtfname" size="40" value="<?=stripslashes($fname);?>"> </font></td> </tr> <tr> <td width="37%" align="right"><font color="red"></font> <font color="#FFFFFF">Middle Name:</font> </td> <td width="63%"><font color="<?=$col?>"> <input type="text" name="txtmname" id="txtmname" size="40" value="<?=stripslashes($mname);?>"> </font></td> </tr> <tr> <td width="37%" align="right"><font color="red">*</font> <font color="#FFFFFF">Last Name:</font> </td> <td width="63%"><font color="<?=$col?>"> <input type="text" name="txtlname" id="txtlname" size="40" value="<?=stripslashes($lname);?>"> </font></td> </tr> <tr> <td width="37%" align="right"><font color="red">*</font> <font color="#FFFFFF">Username:</font> </td> <td width="63%"><font color="<?=$col?>"> <input type="text" name="txtusername" id="txtusername" size="40" value="<?=stripslashes($username);?>"> </font></td> </tr> <tr> <td width="37%" align="right"><font color="red">*</font> <font color="#FFFFFF">Password:</font> </td> <td width="63%"><font color="<?=$col?>"> <input type="text" name="txtpwd" id="txtpwd" size="40" value="<?=stripslashes($pwd);?>"> </font></td> </tr> <tr> <td width="37%" align="right"><font color="red">*</font> <font color="#FFFFFF">Email:</font> </td> <td width="63%"><font color="<?=$col?>"> <input type="text" name="txtemail" id="txtemail" size="40" value="<?=stripslashes($email);?>"> </font></td> </tr> <tr> <td width="37%" align="right"><font color="red"></font> <font color="#FFFFFF">Email 2:</font> </td> <td width="63%"><font color="<?=$col?>"> <input type="text" name="txtemail2" id="txtemail2" size="40" value="<?=stripslashes($email2);?>"> </font></td> </tr> <tr> <td width="37%" align="right"><font color="red"></font> <font color="#FFFFFF">Email 3:</font> </td> <td width="63%"><font color="<?=$col?>"> <input type="text" name="txtemail3" id="txtemail3" size="40" value="<?=stripslashes($email3);?>"> </font></td> </tr> <tr> <td width="37%" align="right"><font color="red">*</font> <font color="#FFFFFF">Date Of Birth:</font> </td> <td width="63%"><font color="<?=$col?>"> <input type="text" name="txtdate_of_birth" readonly="true" id="txtdate_of_birth" size="15" value="<?=stripslashes($gdate);?>"> <a href="#" onClick="getCalendar(document.getElementById('txtdate_of_birth'));"><font color="#FFFFFF"><b>Select Date Here</b></font></a> </font></td> </tr> <tr> <td width="37%" align="right"><font color="red"></font> <font color="#FFFFFF">USSF Referee Registration Number:</font> </td> <td width="63%"><font color="<?=$col?>"> <input type="text" name="txtssn" id="txtssn" size="40" value="<?=stripslashes($ssn);?>"> </font></td> </tr> <tr> <td width="37%" align="right"><font color="red">*</font> <font color="#FFFFFF">Address 1:</font> </td> <td width="63%"><font color="<?=$col?>"> <input type="text" name="txtadd1" id="txtadd1" size="40" value="<?=stripslashes($add1);?>"> </font></td> </tr> <tr> <td width="37%" align="right"><font color="red"></font> <font color="#FFFFFF">Address 2:</font> </td> <td width="63%"><font color="<?=$col?>"> <input type="text" name="txtadd2" id="txtadd2" size="40" value="<?=stripslashes($add2);?>"> </font></td> </tr> <tr> <td width="37%" align="right"><font color="red">*</font> <font color="#FFFFFF">City:</font> </td> <td width="63%"><font color="<?=$col?>"> <input type="text" name="txtcity" id="txtcity" size="40" value="<?=stripslashes($city);?>"> </font></td> </tr> <tr> <td width="37%" align="right"><font color="red">*</font> <font color="#FFFFFF">State:</font> </td> <td width="63%"><font color="<?=$col?>"> <input type="text" name="txtstate" id="txtstate" size="40" value="<?=stripslashes($state);?>"> </font></td> </tr> <tr> <td width="37%" align="right"><font color="red">*</font> <font color="#FFFFFF">Zip:</font> </td> <td width="63%"><font color="<?=$col?>"> <input type="text" name="txtzip" id="txtzip" size="40" value="<?=stripslashes($zip);?>"> </font></td> </tr> <tr> <td width="37%" align="right"><font color="red">*</font> <font color="#FFFFFF">Cell Phone:</font> </td> <td width="63%"><font color="<?=$col?>"> <input type="text" name="txtphone1" id="txtphone1" size="40" value="<?=stripslashes($phone1);?>"> </font></td> </tr> <tr> <td width="37%" align="right"><font color="red"></font> <font color="#FFFFFF">Home Phone:</font> </td> <td width="63%"><font color="<?=$col?>"> <input type="text" name="txtphone2" id="txtphone2" size="40" value="<?=stripslashes($phone2);?>"> </font></td> </tr> <tr> <td> <input type="hidden" value="<?=$_GET["id"]; ?>" name="id"></td> <td><input name="Submit" type="submit" class="buttonclass" value="Submit" onClick="return kyb_check();" /></td> <input type="hidden" name="sub1" value="1"> </tr> <tr><td colspan="2"> </td></tr> </table></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><!--/padding--><!--//col002--></TD> </TR></TBODY></TABLE><!--//body--></DIV></FORM></BODY></HTML> Link to comment https://forums.phpfreaks.com/topic/192183-zip-code-validation/ Share on other sites More sharing options...
idontkno Posted February 15, 2010 Share Posted February 15, 2010 Try this: function validateZip($zip_code) { if(preg_match("/^([0-9]{5})/",$zip_code)) return true; else return false; } Also, if you need to escape strings before putting them into a MySQL database, I would use mysql_real_escape_string(); Link to comment https://forums.phpfreaks.com/topic/192183-zip-code-validation/#findComment-1012818 Share on other sites More sharing options...
miniu Posted February 15, 2010 Author Share Posted February 15, 2010 Thanx it works , but one more thing. How to make it that for example on a drop down menu sort by last name. For example, now I have users registering and they are shown in the order which one registered at first. I want that the drop-down menu will show a list sorted by last name. Link to comment https://forums.phpfreaks.com/topic/192183-zip-code-validation/#findComment-1012832 Share on other sites More sharing options...
idontkno Posted February 15, 2010 Share Posted February 15, 2010 Thanx it works , but one more thing. How to make it that for example on a drop down menu sort by last name. For example, now I have users registering and they are shown in the order which one registered at first. I want that the drop-down menu will show a list sorted by last name. Alphabetically? SELECT * FROM `referees` ORDER BY lname Add a DESC if you want it from Z-A. After re-reading that, I just noticed you wanted it generated. $query = "SELECT lname FROM `referees` ORDER BY lname"; $result = mysql_query($sql); echo "<select>"; while($row = mysql_fetch_array($result)){ echo "<option value=".$row['lname'].">".$row['lname']."</option>"; } echo "</select>"; Link to comment https://forums.phpfreaks.com/topic/192183-zip-code-validation/#findComment-1012841 Share on other sites More sharing options...
miniu Posted February 15, 2010 Author Share Posted February 15, 2010 I have the drop down menu defined like that: <tr> <td width="37%" align="right"><font color="red"></font> Main Referee:</td> <td width="63%"> <font color="<?=$col?>"><select id="mainref" name="mainref" style="width:180px"> <option value="">--Select--</option> <? $sql_mainref="select id,fname,lname from referees where '".$gdate2."' NOT BETWEEN fromdate and todate"; $res_mainref=mysql_query($sql_mainref); while($row_mainref=mysql_fetch_object($res_mainref)) { ?> <option value="<?=$row_mainref->id?>"><?=$row_mainref->fname.' '.$row_mainref->lname?></option> <? } ?> </select> </font></td> </tr> Link to comment https://forums.phpfreaks.com/topic/192183-zip-code-validation/#findComment-1012848 Share on other sites More sharing options...
idontkno Posted February 15, 2010 Share Posted February 15, 2010 I have the drop down menu defined like that: <tr> <td width="37%" align="right"><font color="red"></font> Main Referee:</td> <td width="63%"> <font color="<?=$col?>"><select id="mainref" name="mainref" style="width:180px"> <option value="">--Select--</option> <? $sql_mainref="select id,fname,lname from referees where '".$gdate2."' NOT BETWEEN fromdate and todate"; $res_mainref=mysql_query($sql_mainref); while($row_mainref=mysql_fetch_object($res_mainref)) { ?> <option value="<?=$row_mainref->id?>"><?=$row_mainref->fname.' '.$row_mainref->lname?></option> <? } ?> </select> </font></td> </tr> Change your SQL statement into: $sql_mainref = "SELECT id, fname, lname FROM `referees` WHERE '".$.gdate2."' NOT BETWEEN fromdate AND todate ORDER BY lname"; Link to comment https://forums.phpfreaks.com/topic/192183-zip-code-validation/#findComment-1012852 Share on other sites More sharing options...
miniu Posted February 15, 2010 Author Share Posted February 15, 2010 Great!!!! Thank you so much.... I've learned something today Now I can continue with my work. My developer is gone and has left me with a assignment system and I have to make some changes by my own now. I am still a newbie in php, but I try to learn it every day. Thanx again... Link to comment https://forums.phpfreaks.com/topic/192183-zip-code-validation/#findComment-1012856 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.