PDA

View Full Version : Restrictions on data entered


moe122
02-22-2008, 07:39 PM
hey david,

you know when a user enters data, how do i put restrictions on the data they can enter, such as for username....they have to have atleast 7 characters or letters only, etc. Also if they enter something wrong how do i show they a error message, but making sure that evrything else entered is still on the page.....

davidj
02-23-2008, 11:17 AM
this is known as validation

now there are going to be people who belong to different camps and those being Javascript or PHP. Some reckon that validation should be done using PHP and some Javascript. Both have valid reasons. pro's and con's but I'm right when i say use both....

OK the reason is that the majority will have JS enabled so lets use that technology to catch those people and to point them to where they are going wrong. The other 5% we can use PHP to validate. Yea we have to submit a query to the db but we have reduced the server load by 95% by using JS to catch those that use JS

OK how do we do this...

in javascript you need a function


function checkform(){

/// get field value
fld1 = document.getElementById('field1').value;

/// detect length
if(fld1.length > 6){

/// submit form if greater than 6
document.forms[0].submit()

}else{

/// alert if not greater than 6
alert('you have entered incorrect data');

}

}


OK

to call the function assign it to the submit button but first change that button from a submit to a standard button by changing its type value to button

add the event...

onclick="checkform()" to the button tag

moe122
02-24-2008, 02:20 PM
ok i need to write some rules/validations on some fields like

1 text only then enter into database
2 if both field match .........
3 must contain '@'......
4 number only
5 if this option is selected enter such a field in.....

and what validation would you have on postcode?

i think it goes in this 1 topic right?

davidj
02-24-2008, 02:51 PM
ok

you need to write this in JS but also check it in PHP

/////////// javascript /////////////


1 text only then enter into database


in JS you have a function called ...

isNaN // isNotANumber

this is a boolean and will return true or false

example...

number = 200;
test = isNaN(number);

if(test){

do stuff here as test == false

}


///////////////

2 if both field match .........

you can use the following


a = document.getElementById('field1').value;
b = document.getElementById('field2').value;

if(a == b){

a and b match so do stuff

}


////////////////

3 must contain '@'......




var haystack="dave.j@dreamweaverclub.com (haystack=dave.j@dreamweaverclub.com)"

if (haystack.indexOf("@")!=-1){
// do stuff here because i have found an @
}


///////////////////////
4 number only


number = 200;
test = !isNaN(number);

if(test){

do stuff here as test == true

}


and the last one ill let you have a go at

impress me

moe122
02-24-2008, 03:05 PM
do i need to incorporate that into the following code

if($Submit && $First_Name && $Surname && $Username && $Password && $Email && $Gender && $Date_of_Birth && $City && $Region && $Country && $Postcode && $Religion && $Ethnic_Origin && $Phone_Number && $Marital_Status && $Number_of_Children && $Occupation){
/////
$query = sprintf("INSERT into clients (First_Name, Surname, Username, Password, Email, Gender, Date_of_Birth, City, Region, Country, Postcode, Religion, Ethnic_Origin, Phone_Number, Marital_Status, Number_of_Children, Occupation) values ('$First_Name', '$Surname', '$Username', '$Password', '$Email', '$Gender', '$Date_of_Birth', '$City', '$Region', '$Country', '$Postcode', '$Religion', '$Ethnic_Origin', '$Phone_Number', '$Marital_Status', '$Number_of_Children', '$Occupation')");
mysql_query ($query) or die (mysql_error());
/////





or have it on its own?

davidj
02-24-2008, 03:17 PM
its in javascript

you need to cover the basics in javascript

moe122
02-24-2008, 03:20 PM
i havent got much time to do that, im already way behind.

moe122
02-24-2008, 03:34 PM
could you explain how i would do 1 of them, so i get the concept

davidj
02-24-2008, 03:52 PM
im sorry but there is no quick way to do this

i cant do this for you so your going to have to find another way if you dont have the time to learn

moe122
02-24-2008, 04:31 PM
so i write the function in js but what do you mean by check it in php?

davidj
02-24-2008, 04:40 PM
you do the same in php

moe122
02-24-2008, 04:59 PM
what am i doing wrong here?

<script language="Javascript" type="text/javascript">
<!--
function checkformlength(){

/// get field value
fld1 = document.getElementById('Username').value;
fld2 = document.getElementById('Password').value;

/// detect length
if(fld1.length > 6 && fld2.length >6){
/// submit form if greater than 6
document.forms[0].submit()

}else{

/// alert if not greater than 6
alert('Username/Password must contain more than 6 characters');

}

}
//-->
</script>
<?php
require_once("Connections/Connection.php"); // Database Connection
$First_Name = $_POST['First_Name'];
$Surname = $_POST['Surname'];
$Username = $_POST['Username'];
$Password = $_POST['Password'];
$Email = $_POST['Email'];
$Gender = $_POST['Gender'];
$Day = $_POST['Day'];
$Month = $_POST['Month'];
$Year = $_POST['Year'];
$Date_of_Birth = "$Year-$Month-$Day"; // << concatenate the date parts
$City = $_POST['City'];
$Region = $_POST['Region'];
$Country = $_POST['Country'];
$Postcode = $_POST['Postcode'];
$Religion = $_POST['Religion'];
$Ethnic_Origin = $_POST['Ethnic_Origin'];
$Occupation = $_POST['Occupation'];
$Marital_Status = $_POST['Marital_Status'];
$Number_of_Children = $_POST['Number_of_Children'];
$Phone_Number =$_POST['Phone_Number'];
$Submit = $_POST['Submit'];
$error = "One of your fields is empty !";
if($Submit && $First_Name && $Surname && $Username && $Password && $Email && $Gender && $Date_of_Birth && $City && $Region && $Country && $Postcode && $Religion && $Ethnic_Origin && $Phone_Number && $Marital_Status && $Number_of_Children && $Occupation){
/////
$query = sprintf("INSERT into clients (First_Name, Surname, Username, Password, Email, Gender, Date_of_Birth, City, Region, Country, Postcode, Religion, Ethnic_Origin, Phone_Number, Marital_Status, Number_of_Children, Occupation) values ('$First_Name', '$Surname', '$Username', '$Password', '$Email', '$Gender', '$Date_of_Birth', '$City', '$Region', '$Country', '$Postcode', '$Religion', '$Ethnic_Origin', '$Phone_Number', '$Marital_Status', '$Number_of_Children', '$Occupation')");
mysql_query ($query) or die (mysql_error());
/////
}elseif($Submit) {
echo $error;
}
/////
$query = sprintf("SELECT * FROM clients");
$result = @mysql_query($query);
$row = @mysql_fetch_array($result);
/////
?>
<!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>Form</title>
<style type="text/css">
<!--
.style1 {color: #000000}
.style5 {font-size: 24px}
-->
</style>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<table width="563" border="0">
<tr bordercolor="0">
<td colspan="3" bgcolor="#E4E4E4"><span class="style5">Personal Details</span></td>
</tr>
<tr bordercolor="0">
<td width="266" bgcolor="#F1F1F1"><span class="style1">First Name</span></td>
<td width="123" bgcolor="#FAFAFA"><label>
<input type="text" name="First_Name" id="First_Name" />
</label></td>
<td width="160" bgcolor="#FAFAFA">&nbsp;</td>
</tr>
<tr bordercolor="0">
<td bgcolor="#F1F1F1"><span class="style1">Surname</span></td>
<td bgcolor="#FAFAFA"><label>
<input type="text" name="Surname" id="Surname" />
</label></td>
<td bgcolor="#FAFAFA">&nbsp;</td>
</tr>
<tr bordercolor="0">
<td bgcolor="#F1F1F1"><span class="style1">Username</span></td>
<td bgcolor="#FAFAFA"><label>
<input type="text" name="Username" id="Username" />
</label></td>
<td bgcolor="#FAFAFA">&nbsp;</td>
</tr>
<tr bordercolor="0">
<td bgcolor="#F1F1F1">Password</td>
<td bgcolor="#FAFAFA"><label>
<input type="password" name="Password" id="Password" />
</label></td>
<td bgcolor="#FAFAFA">&nbsp;</td>
</tr>
<tr bordercolor="0">
<td bgcolor="#F1F1F1"><span class="style1">Verify Password</span></td>
<td bgcolor="#FAFAFA"><label>
<input type="password" name="Verify_Password" id="Verify_Password" />
</label></td>
<td bgcolor="#FAFAFA">&nbsp;</td>
</tr>
<tr bordercolor="0">
<td bgcolor="#F1F1F1"><span class="style1">Email Address</span></td>
<td bgcolor="#FAFAFA"><label>
<input type="text" name="Email" id="Email" />
</label></td>
<td bgcolor="#FAFAFA">&nbsp;</td>
</tr>
<tr bordercolor="0">
<td bgcolor="#F1F1F1"><span class="style1">Gender</span></td>
<td bgcolor="#FAFAFA"><label>
<select name="Gender" id="Gender">
<option>Male</option>
<option>Female</option>
</select>
</label></td>
<td bgcolor="#FAFAFA">&nbsp;</td>
</tr>
<tr bordercolor="0">
<td bgcolor="#F1F1F1"><span class="style1">Date of Birth</span></td>
<td bgcolor="#FAFAFA">Day
<select name="Day" id="Day">
<option selected="selected">01</option>
<option>02</option>
<option>03</option>
<option>04</option>
<option>05</option>
<option>06</option>
<option>07</option>
<option>08</option>
<option>09</option>
<option>10</option>
<option>11</option>
<option>12</option>
<option>13</option>
<option>14</option>
<option>15</option>
<option>16</option>
<option>17</option>
<option>18</option>
<option>19</option>
<option>20</option>
<option>21</option>
<option>22</option>
<option>23</option>
<option>24</option>
<option>25</option>
<option>26</option>
<option>27</option>
<option>28</option>
<option>29</option>
<option>30</option>
<option>31</option>
</select>
Month
<select name="Month" id="Month">
<option>01</option>
<option>02</option>
<option>03</option>
<option>04</option>
<option>05</option>
<option>06</option>
<option>07</option>
<option>08</option>
<option>09</option>
<option>10</option>
<option>11</option>
<option>12</option>
</select>
Year
<input name="Year" type="text" id="Year" value="YYYY" size="2" maxlength="4" /></td>
<td bgcolor="#FAFAFA">&nbsp;</td>
</tr>
<tr bordercolor="0">
<td bgcolor="#F1F1F1"><span class="style1">City</span></td>
<td bgcolor="#FAFAFA"><label>
<input type="text" name="City" id="City" />
</label></td>
<td bgcolor="#FAFAFA">&nbsp;</td>
</tr>
<tr bordercolor="0">
<td bgcolor="#F1F1F1"><span class="style1">Region</span></td>
<td bgcolor="#FAFAFA"><label>
<input type="text" name="Region" id="Region" />
</label></td>
<td bgcolor="#FAFAFA">&nbsp;</td>
</tr>
<tr bordercolor="0">
<td bgcolor="#F1F1F1"><span class="style1">Country</span></td>
<td bgcolor="#FAFAFA"><label>
<select name="Country" id="Country">
<option>Afghanistan</option>
<option>Argentina</option>
<option>Australia</option>
<option>Austria</option>
<option>Bangladesh</option>
<option>Belgium</option>
<option>Bolivia</option>
<option>Brazil</option>
<option>Bulgaria</option>
<option>Canada</option>
<option>Chile</option>
<option>China</option>
<option>Colombia</option>
<option>Costa Rica</option>
<option>Croatia</option>
<option>Czech Republic</option>
<option>Denmark</option>
<option>Dominican Republic</option>
<option>Ecuador</option>
<option>Egypt</option>
<option>El Salvador</option>
<option>Estonia</option>
<option>Europe</option>
<option>Finland</option>
<option>France</option>
<option>Germany</option>
<option>Greece</option>
<option>Guatemala</option>
<option>Honduras</option>
<option>Hong Kong</option>
<option>Hungary</option>
<option>India</option>
<option>Indonesia</option>
<option>Iraq</option>
<option>Ireland</option>
<option>Israel</option>
<option>Italy</option>
<option>Jamaica</option>
<option>Japan</option>
<option>Jordan</option>
<option>Kenya</option>
<option>Korea</option>
<option>Kazakhstan</option>
<option>Latvia</option>
<option>Lithuania</option>
<option>Latin America</option>
<option>Luxembourg</option>
<option>Macedonia</option>
<option>Malaysia</option>
<option>Mexico</option>
<option>Moldova</option>
<option>Morocco</option>
<option>Netherlands</option>
<option>New Zealand</option>
<option>Norway</option>
<option>Pakistan</option>
<option>Panama</option>
<option>Paraguay</option>
<option>Peru</option>
<option>Philippines</option>
<option>Poland</option>
<option>Portugal</option>
<option>Puerto Rico</option>
<option>Romania</option>
<option>Russia</option>
<option>Saudi Arabia</option>
<option>Serbia and Montenegro</option>
<option>Singapore</option>
<option>Slovakia</option>
<option>Slovenia</option>
<option>South Africa</option>
<option>Spain</option>
<option>Sri Lanka</option>
<option>Sweden</option>
<option>Switzerland</option>
<option>Syria</option>
<option>Taiwan</option>
<option>Thailand</option>
<option>Trinidad and Tobago</option>
<option>Tunisia</option>
<option>Turkey</option>
<option>Ukraine</option>
<option>United Kingdom</option>
<option>United States</option>
<option>Uruguay</option>
<option>Venezuela</option>
<option>Vietnam</option>
<option>Other (Please Specify)</option>
</select>
</label></td>
<td bgcolor="#FAFAFA"><label>
<input type="text" name="Country_Other" id="Country_Other" />
</label></td>
</tr>
<tr bordercolor="0">
<td bgcolor="#F1F1F1"><span class="style1">Postcode</span></td>
<td bgcolor="#FAFAFA"><label>
<input type="text" name="Postcode" id="Postcode" />
</label></td>
<td bgcolor="#FAFAFA">&nbsp;</td>
</tr>
<tr bordercolor="0">
<td bgcolor="#F1F1F1"><span class="style1">Religion</span></td>
<td bgcolor="#FAFAFA"><label>
<select name="Religion" id="Religion">
<option>Sunni Hanafi</option>
<option>Sunni Humblie</option>
<option>Sunni Maliki</option>
<option>Sunni Shafi</option>
<option>Shia</option>
<option>Just a Muslim</option>
<option>Buddhism</option>
<option>Christianity</option>
<option>Judaism</option>
<option>Hinduism</option>
<option>Sikhism</option>
<option>Other (Please Specify)</option>
</select>
</label></td>
<td bgcolor="#FAFAFA"><label>
<input type="text" name="Religion_Other" id="Religion_Other" />
</label></td>
</tr>
<tr bordercolor="0">
<td bgcolor="#F1F1F1"><span class="style1">Ethnic Origin</span></td>
<td bgcolor="#FAFAFA"><label>
<select name="Ethnic_Origin" id="Ethnic_Origin">
<option>White English</option>
<option>White Irish</option>
<option>White Scottish</option>
<option>White Welsh</option>
<option>White Other</option>
<option>Asain Bangladeshi</option>
<option>Asain Indian</option>
<option>Asain Pakistani</option>
<option>Asain Other</option>
<option>Black African</option>
<option>Black Carribean</option>
<option>Black Other</option>
<option>Mixed (Please Specify)</option>
<option>Other (Please Specify)</option>
</select>
</label></td>
<td bgcolor="#FAFAFA"><label>
<input type="text" name="Ethnic Origin_Mixed_Other" id="Ethnic Origin_Mixed_Other" />
</label></td>
</tr>
<tr bordercolor="0">
<td bgcolor="#F1F1F1"><span class="style1">Occupation</span></td>
<td bgcolor="#FAFAFA"><label>
<input type="text" name="Occupation" id="Occupation" />
</label></td>
<td bgcolor="#FAFAFA">&nbsp;</td>
</tr>
<tr bordercolor="0">
<td bgcolor="#F1F1F1"><span class="style1">Marital Status</span></td>
<td bgcolor="#FAFAFA"><label>
<select name="Marital_Status" id="Marital_Status">
<option>Married</option>
<option>Single</option>
<option>Separated</option>
<option>Divorved</option>
<option>Widowed</option>
</select>
</label></td>
<td bgcolor="#FAFAFA">&nbsp;</td>
</tr>
<tr bordercolor="0">
<td bgcolor="#F1F1F1"><span class="style1">Number of Children (if any)</span></td>
<td bgcolor="#FAFAFA"><label>
<select name="Number_of_Children" id="Number_of_Children">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="More than 5">More than 5</option>
</select>
</label></td>
<td bgcolor="#FAFAFA">&nbsp;</td>
</tr>
<tr bordercolor="0">
<td bgcolor="#F1F1F1"><span class="style1">Phone Number (will only be used by us to contact you and will not be shown to any third parties)</span></td>
<td bgcolor="#FAFAFA"><label>
<input type="text" name="Phone_Number" id="Phone_Number" />
</label></td>
<td bgcolor="#FAFAFA">&nbsp;</td>
</tr>
<tr bordercolor="0">
<td bordercolor="0" bgcolor="#F1F1F1">&nbsp;</td>
<td bordercolor="0" bgcolor="#FAFAFA"><label>
<input type="button" name="Submit" id="Submit" value="Submit" onclick="checkform()" />
<input name="Submit" type="hidden" id="Submit" value="1" />
</label></td>
<td bgcolor="#FAFAFA">&nbsp;</td>
</tr>
</table>
</form>
</body>
</html>