simple_man_11 Posted April 23, 2007 Share Posted April 23, 2007 Does anyone know how to validate a field (login) in a form that has to be the following: No special characters at all no spaces Here is my php code, it is checking for empty already. <?php if(isset($_POST['Submit'])) { // all fields are required if(empty($_POST['uname']) || (empty($_POST['pw1'])) || (empty($_POST['pw2'])) || (empty($_POST['fullname'])) || (empty($_POST['address'])) || (empty($_POST['city'])) || (empty($_POST['state'])) || (empty($_POST['zip'])) || (empty($_POST['email'])) || (empty($_POST['age'])) || (empty($_POST['sex'])) || (empty($_POST['height'])) || (empty($_POST['weight'])) || (empty($_POST['pregnant']))|| (empty($_POST['cardo'])) || (empty($_POST['resistance'])) || (empty($_POST['familyheartdisease'])) || (empty($_POST['familysuddendeath'])) || (empty($_POST['familyhighblood'])) || (empty($_POST['familydiabetes'])) || (empty($_POST['familystroke'])) ||(empty($_POST['highblood'])) ||(empty($_POST['heartproblems'])) ||(empty($_POST['heartdisease'])) ||(empty($_POST['arthritis'])) ||(empty($_POST['asthma'])) ||(empty($_POST['highcholesterol'])) ||(empty($_POST['bruiseorbleed'])) ||(empty($_POST['heatillness'])) ||(empty($_POST['stroke'])) ||(empty($_POST['diabetes'])) ||(empty($_POST['usesauna']))||(empty($_POST['saunatype'])) ) { header("Location:Messages.php?msg=14"); exit(); } $name=$_POST['name']; $email=$_POST['email']; $pw1=$_POST['pw1']; $pw2=$_POST['pw2']; if("$pw1" !== "$pw2" ) { header( "Location:Messages.php?msg=5" ); exit(); } $ip = $_SERVER['REMOTE_ADDR']; //connect to the db server , check if username exist include('config.php'); $query=("Select * from users where username='$uname'"); $result= mysql_query($query); $num=mysql_num_rows($result); if ($num > 0) { //Username already exist header( "Location:Messages.php?msg=6" ); exit(); } else { //if username does not exist insert user details $query = ( "INSERT INTO users (username, email, name, password, sex, city, state, zip, address, age, height, weight, pregnant, cardo, resistance, familyheartdisease, familysuddendeath, familyhighblood, familydiabetes, familystroke, highblood, heartproblems, heartdisease, arthritis, asthma, highcholesterol, bruiseorbleed, heatillness, stroke, diabetes, membersince) VALUES ('$uname','$email','$fullname','$pw1', '$sex', '$city', '$state', '$zip', '$address', '$age', '$height', '$weight', '$pregnant', '$cardo', '$resistance', '$familyheartdisease', '$familysuddendeath', '$familyhighblood', '$familydiabetes', '$familystroke', '$highblood', '$heartproblems', '$heartdisease', '$arthritis', '$asthma', '$highcholesterol', '$bruiseorbleed', '$heatillness', '$stroke', '$diabetes', '$usesauna','$saunatype' ,NOW() )"); $result = mysql_query($query); if (!$result) { $message = 'Invalid query: ' . mysql_error() . "\n"; $message .= 'Whole query: ' . $query; die($message); } else { /////////////////create folder with username and copy files within//////////////////////////////// $userdirectory = "./users/$uname"; mkdir($userdirectory, 0777); copy("./userstuff/hr.txt","$userdirectory/hr.txt"); copy("./userstuff/q1.htm","$userdirectory/q1.htm"); copy("./userstuff/q2.htm","$userdirectory/q2.htm"); copy("./userstuff/graphtest.php","$userdirectory/graphtest.php"); copy("./userstuff/voti.html","$userdirectory/voti.html"); copy("./userstuff/voti.swf","$userdirectory/voti.swf"); copy("./userstuff/voti2.swf","$userdirectory/voti2.swf"); copy("./userstuff/voti3.swf","$userdirectory/voti3.swf"); copy("./userstuff/voti4.swf","$userdirectory/voti4.swf"); copy("./userstuff/q1.txt","$userdirectory/q1.txt"); copy("./userstuff/q2.txt","$userdirectory/q2.txt"); copy("./userstuff/q3.txt","$userdirectory/q3.txt"); copy("./userstuff/q4.txt","$userdirectory/q4.txt"); copy("./userstuff/q2.php","$userdirectory/q2.php"); //copy("config.php","$uname/config.php"); //copy("login.php","$uname/login.php"); //copy("logout.php","$uname/logout.php"); /////////////////////////////////////////////////////////////////////////////////////////////////// //echo "Successfully added!!!"; header( "Location:Messages.php?msg=13" ); exit(); } //if (@mysql_query ($query)) { //header("location:login.php?reg=1"); //exit; //} } //mysql_close(); } ?> Html form <form name="form1" action="register.php" method="post"> <table id="frame" cellpadding="0" cellspacing="0" border="0" align="center"> <tr> <td valign="top" width="766" height="210"><div align="center"></div> <table width="639" align="center"> <tr> <td width="121"><span class="style1">Username</span></td> <td width="202"><input type="text" name="uname"></td> <td width="109"><span class="style1"> <label>Zip Code</label> </span></td> <td width="189"><input name="zip" type="text" size="8" maxlength="5"></td> </tr> <tr> <td><span class="style1">Password</span></td> <td><input type="password" name="pw1"></td> <td><span class="style1">Email</span></td> <td><input type="text" name="email"></td> </tr> <tr> <td><span class="style1">Confirm Password</span></td> <td><input type="password" name="pw2"></td> <td><span class="style1">Year of Birth </span></td> <td> <select name="age" class="inputstyle" style="width:144px" onChange=checkType(this) id='typ'> <option value="1937" >1937</option> <option value="1938" >1938</option> <option value="1939" >1939</option> <option value="1940" >1940</option> <option value="1941" >1941</option> <option value="1942" >1942</option> <option value="1943" >1943</option> <option value="1944" >1944</option> <option value="1945" >1945</option> <option value="1946" >1946</option> <option value="1947" >1947</option> <option value="1948" >1948</option> <option value="1949" >1949</option> <option value="1950" >1950</option> <option value="1951" >1951</option> <option value="1952" >1952</option> <option value="1953" >1953</option> <option value="1954" >1954</option> <option value="1955" >1955</option> <option value="1956" >1956</option> <option value="1957" >1957</option> <option value="1958" >1958</option> <option value="1959" >1959</option> <option value="1960" >1960</option> <option value="1961" >1961</option> <option value="1962" >1962</option> <option value="1963" >1963</option> <option value="1964" >1964</option> <option value="1965" >1965</option> <option value="1966" >1966</option> <option value="1967" >1967</option> <option value="1968" >1968</option> <option value="1969" >1969</option> <option value="1970" >1970</option> <option value="1971" >1971</option> <option value="1972" >1972</option> <option value="1973" >1973</option> <option value="1974" >1974</option> <option value="1975" >1975</option> <option value="1976" >1976</option> <option value="1977" >1977</option> <option value="1978" >1978</option> <option value="1979" >1979</option> <option value="1980" >1980</option> <option value="1981" >1981</option> <option value="1982" >1982</option> <option value="1983" >1983</option> <option value="1984" >1984</option> <option value="1985" >1985</option> <option value="1986" >1986</option> <option value="1987" >1987</option> <option value="1988" >1988</option> <option value="1989" >1989</option> <option value="1990" >1990</option> <option value="1991" >1991</option> <option value="1992" >1992</option> <option value="1993" >1993</option> <option value="1994" >1994</option> <option value="1995" >1995</option> <option value="1996" >1996</option> <option value="1997" >1997</option> </select> </td> </tr> <tr> <td><span class="style1">Full Name</span></td> <td><input type="text" name="fullname"></td> <td><span class="style1">Gender</span></td> <td><select name="sex" class="inputstyle" style="width:144px" onChange=checkType(this) id='typ'> <option value=" " selected>Please select gender</option> <option value="Female" >Female</option> <option value="Male" >Male</option> </select></td> </tr> <tr> <td><span class="style1">Address</span></td> <td><input type="text" name="address"></td> <td><span class="style1">Height in inches</span></td> <td><input type="text" name="height"></td> </tr> <tr> <td><span class="style1">City</span></td> <td><input type="text" name="city"></td> <td><span class="style1">Weight in pounds</span></td> <td><input type="text" name="weight"></td> </tr> <tr> <td><span class="style1">State</span></td> <td><!--<input type="text" name="state">--> <select name="state" style="width:55px" onChange=checkType(this) id='typ'> <option value="AL">AL</option> <option value="AK">AK</option> <option value="AZ">AZ</option> <option value="AR">AR</option> <option value="CA">CA</option> <option value="CO">CO</option> <option value="CT">CT</option> <option value="DE">DE</option> <option value="FL">FL</option> <option value="GA">GA</option> <option value="HI">HI</option> <option value="ID">ID</option> <option value="IL">IL</option> <option value="IN">IN</option> <option value="IA">IA</option> <option value="KS">KS</option> <option value="KY">KY</option> <option value="LA">LA</option> <option value="ME">ME</option> <option value="MD">MD</option> <option value="MA">MA</option> <option value="MI">MI</option> <option value="MN">MN</option> <option value="MS">MS</option> <option value="MO">MO</option> <option value="MT">MT</option> <option value="NE">NE</option> <option value="NV">NV</option> <option value="NH">NH</option> <option value="NJ">NJ</option> <option value="NM">NM</option> <option value="NY">NY</option> <option value="NC">NC</option> <option value="ND">ND</option> <option value="OH">OH</option> <option value="OK">OK</option> <option value="OR">OR</option> <option value="PA">PA</option> <option value="RI">RI</option> <option value="SC">SC</option> <option value="SD">SD</option> <option value="TN">TN</option> <option value="TX">TX</option> <option value="UT">UT</option> <option value="VT">VT</option> <option value="VA">VA</option> <option value="WA">WA</option> <option value="WV">WV</option> <option value="WI">WI</option> <option value="WY">WY</option> </select> </td> <td> </td> <td> </td> </tr> </table></td> </tr> </table> <table cellpadding="0" cellspacing="0" border="0" align="center"> <tr> <td valign="top" width="766" height="400"><table width="641" border="1" align="center" bordercolor="#CCCCCC"> <tr> <td width="219" bgcolor="#CCCCCC">Are you pregnant? </td> <td width="78" bgcolor="#CCCCCC"><input name="pregnant" type="radio" value="yes"> Yes</td> <td width="78" bgcolor="#CCCCCC"><label> <input name="pregnant" type="radio" value="no"> No </label></td> <td width="106" bgcolor="#CCCCCC"> </td> <td width="138" bgcolor="#CCCCCC"> </td> </tr> <tr> <td>Cardiovascular exercise </td> <td><input name="cardo" type="radio" value="no"> No </td> <td><input name="cardo" type="radio" value="light"> Light </td> <td><input name="cardo" type="radio" value="moderate"> Moderate </td> <td><input name="cardo" type="radio" value="heavy"> Heavy</td> </tr> <tr> <td bgcolor="#CCCCCC">Resistance training </td> <td bgcolor="#CCCCCC"><input name="resistance" type="radio" value="no"> No </td> <td bgcolor="#CCCCCC"><input name="resistance" type="radio" value="light"> Light </td> <td bgcolor="#CCCCCC"><input name="resistance" type="radio" value="moderate"> Moderate</td> <td bgcolor="#CCCCCC"><input name="resistance" type="radio" value="heavy"> Heavy</td> </tr> <tr> <td>Heart disease in family? </td> <td><input name="familyheartdisease" type="radio" value="yes"> Yes</td> <td><input name="familyheartdisease" type="radio" value="no"> No</td> <td> </td> <td> </td> </tr> <tr> <td bgcolor="#CCCCCC">Sudden death in family? </td> <td bgcolor="#CCCCCC"><input name="familysuddendeath" type="radio" value="yes"> Yes</td> <td bgcolor="#CCCCCC"><input name="familysuddendeath" type="radio" value="no"> No</td> <td bgcolor="#CCCCCC"> </td> <td bgcolor="#CCCCCC"> </td> </tr> <tr> <td>High blood pressure in family? </td> <td><input name="familyhighblood" type="radio" value="yes"> Yes</td> <td><input name="familyhighblood" type="radio" value="no"> No</td> <td> </td> <td> </td> </tr> <tr> <td bgcolor="#CCCCCC">Diabetes in family?</td> <td bgcolor="#CCCCCC"><input name="familydiabetes" type="radio" value="yes"> Yes</td> <td bgcolor="#CCCCCC"><input name="familydiabetes" type="radio" value="no"> No</td> <td bgcolor="#CCCCCC"> </td> <td bgcolor="#CCCCCC"> </td> </tr> <tr> <td>Stroke in family? </td> <td><input name="familystroke" type="radio" value="yes"> Yes</td> <td><input name="familystroke" type="radio" value="no"> No</td> <td> </td> <td> </td> </tr> <tr> <td bgcolor="#CCCCCC">Do you have high blood pressure?</td> <td bgcolor="#CCCCCC"><input name="highblood" type="radio" value="yes"> Yes</td> <td bgcolor="#CCCCCC"><input name="highblood" type="radio" value="no"> No</td> <td bgcolor="#CCCCCC"> </td> <td bgcolor="#CCCCCC"> </td> </tr> <tr> <td>Do you have heart problems?</td> <td><input name="heartproblems" type="radio" value="yes"> Yes</td> <td><input name="heartproblems" type="radio" value="no"> No</td> <td> </td> <td> </td> </tr> <tr> <td bgcolor="#CCCCCC">Do you have heart disease?</td> <td bgcolor="#CCCCCC"><input name="heartdisease" type="radio" value="yes"> Yes</td> <td bgcolor="#CCCCCC"><input name="heartdisease" type="radio" value="no"> No</td> <td bgcolor="#CCCCCC"> </td> <td bgcolor="#CCCCCC"> </td> </tr> <tr> <td>Do you have arthritis? </td> <td><input name="arthritis" type="radio" value="yes"> Yes</td> <td><input name="arthritis" type="radio" value="no"> No</td> <td> </td> <td> </td> </tr> <tr> <td bgcolor="#CCCCCC">Do you have asthma? </td> <td bgcolor="#CCCCCC"><input name="asthma" type="radio" value="yes"> Yes</td> <td bgcolor="#CCCCCC"><input name="asthma" type="radio" value="no"> No</td> <td bgcolor="#CCCCCC"> </td> <td bgcolor="#CCCCCC"> </td> </tr> <tr> <td>Do you have high cholesterol? </td> <td><input name="highcholesterol" type="radio" value="yes"> Yes</td> <td><input name="highcholesterol" type="radio" value="no"> No</td> <td> </td> <td> </td> </tr> <tr> <td bgcolor="#CCCCCC">Do you bruise or bleed easily? </td> <td bgcolor="#CCCCCC"><input name="bruiseorbleed" type="radio" value="yes"> Yes</td> <td bgcolor="#CCCCCC"><input name="bruiseorbleed" type="radio" value="no"> No</td> <td bgcolor="#CCCCCC"> </td> <td bgcolor="#CCCCCC"> </td> </tr> <tr> <td>Do you suffer from heat illness?</td> <td><input name="heatillness" type="radio" value="yes"> Yes</td> <td><input name="heatillness" type="radio" value="no"> No</td> <td> </td> <td> </td> </tr> <tr> <td bgcolor="#CCCCCC">Have you ever had a stroke? </td> <td bgcolor="#CCCCCC"><input name="stroke" type="radio" value="yes"> Yes</td> <td bgcolor="#CCCCCC"><input name="stroke" type="radio" value="no"> No</td> <td bgcolor="#CCCCCC"> </td> <td bgcolor="#CCCCCC"> </td> </tr> <tr> <td>Do you have diabetes? </td> <td><input name="diabetes" type="radio" value="yes"> Yes</td> <td><input name="diabetes" type="radio" value="no"> No</td> <td> </td> <td> </td> </tr> <tr> <td bgcolor="#CCCCCC">Do you use a Sauna? </td> <td bgcolor="#CCCCCC"><input name="usesauna" type="radio" value="no" /> No </td> <td bgcolor="#CCCCCC"><input name="usesauna" type="radio" value="light" /> Light </td> <td bgcolor="#CCCCCC"><input name="usesauna" type="radio" value="moderate" /> Moderate</td> <td bgcolor="#CCCCCC"><input name="usesauna" type="radio" value="heavy" /> Heavy</td> </tr> <tr> <td>What type of Sauna? </td> <td><input name="saunatype" type="radio" value="Infared" /> Infared</td> <td><input name="saunatype" type="radio" value="Steam" /> Steam</td> <td><input name="saunatype" type="radio" value="Hotrock" /> Hotrock</td> <td><input name="saunatype" type="radio" value="Other" /> Other</td> </tr> </table> <table width="685" border="0"> <tr> <td width="314" height="96"><p> </p> <p> </p></td> <td width="70"><input name="Submit" type="submit" value="Register" /></td> <td width="49"><input type="reset" name="Submit2" value="Reset" /></td> <td width="234"> </td> </tr> </table> </form> Quote Link to comment Share on other sites More sharing options...
steelmanronald06 Posted April 23, 2007 Share Posted April 23, 2007 You will need regex to help you here. I'm moving you to the regex help board. Quote Link to comment Share on other sites More sharing options...
effigy Posted April 23, 2007 Share Posted April 23, 2007 I suggest cleaning up your code with a foreach first. What do you mean by special characters? Non-ASCII? Quote Link to comment Share on other sites More sharing options...
simple_man_11 Posted April 23, 2007 Author Share Posted April 23, 2007 first question, what do you mean clean it to a foreach? Could you give me an example? Secondly, yea Non-ASCII AND NO SPACES. I just want it to be numbers and letters with no spaces Quote Link to comment Share on other sites More sharing options...
effigy Posted April 23, 2007 Share Posted April 23, 2007 Something like this should work: if (isset($_POST['Submit'])) { unset($_POST['Submit']); foreach ($_POST as $key => $value) { if (empty($value)) { header("Location:Messages.php?msg=14"); exit(); } } } To match only ASCII letters and numbers use /^[A-Za-z0-9]+\z/ with preg_match. Quote Link to comment Share on other sites More sharing options...
simple_man_11 Posted April 23, 2007 Author Share Posted April 23, 2007 but that will not take care of spaces will it? I can not have a username as bob 1 it must be bob1 Quote Link to comment Share on other sites More sharing options...
effigy Posted April 23, 2007 Share Posted April 23, 2007 The pattern only allows A-Z, a-z, and 0-9 in the string. If you ran "bob 1" against it, preg_match would return 0 (false). Quote Link to comment Share on other sites More sharing options...
simple_man_11 Posted April 23, 2007 Author Share Posted April 23, 2007 so would this work then? if (preg_match("/^[A-Za-z0-9]+\z/", $uname) { echo "Ok" ; } else { echo "not ok"; } Quote Link to comment Share on other sites More sharing options...
effigy Posted April 23, 2007 Share Posted April 23, 2007 You can always try it out... (Yes, if you use the right syntax--the if needs a closing paren.) Quote Link to comment Share on other sites More sharing options...
simple_man_11 Posted April 23, 2007 Author Share Posted April 23, 2007 Thanks. I will try it out tonight, the only reason why I didn't try it out yet was because I am not at my working computer. I did notice that I didn't include the final ). thanks for all your help and if that fixes things I will close this topic. Also thanks for showing me a better way on my isEmpty code as well... 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.