logo-dw

Go Back   Dreamweaver Club Forums > Dreamweaver forums > Video Tutorials
Register FAQ Members List Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Display Modes
Old 02-22-2008, 06:39 PM   #1
moe122
 
Join Date: Feb 2008
Posts: 138
Default Restrictions on data entered

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.....
moe122 is offline   Reply With Quote
Old 02-23-2008, 10:17 AM   #2
davidj
davidj's Avatar
 
Join Date: Sep 2005
Location: The Toon (newcastle upon Tyne)
Posts: 8,256
Default

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

Code:
 
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...

Code:
onclick="checkform()"
to the button tag
__________________
Would you like to learn PHP from me? Check out -> www.codezenith.co.uk
davidj is offline   Reply With Quote
Old 02-24-2008, 01:20 PM   #3
moe122
 
Join Date: Feb 2008
Posts: 138
Default

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?
moe122 is offline   Reply With Quote
Old 02-24-2008, 01:51 PM   #4
davidj
davidj's Avatar
 
Join Date: Sep 2005
Location: The Toon (newcastle upon Tyne)
Posts: 8,256
Default

ok

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

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

Quote:
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...
Code:
 
number = 200;
test = isNaN(number);
 
if(test){
 
do stuff here as test == false
 
}
///////////////
Quote:
2 if both field match .........
you can use the following

Code:
 
a = document.getElementById('field1').value;
b = document.getElementById('field2').value;
 
if(a == b){
 
a and b match so do stuff
 
}
////////////////
Quote:
3 must contain '@'......

Code:
 
var haystack="dave.j@dreamweaverclub.com"
 
if (haystack.indexOf("@")!=-1){
 // do stuff here because i have found an @
}
///////////////////////
Quote:
4 number only
Code:
 
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
__________________
Would you like to learn PHP from me? Check out -> www.codezenith.co.uk
davidj is offline   Reply With Quote
Old 02-24-2008, 02:05 PM   #5
moe122
 
Join Date: Feb 2008
Posts: 138
Default

do i need to incorporate that into the following code

PHP 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?
moe122 is offline   Reply With Quote
Old 02-24-2008, 02:17 PM   #6
davidj
davidj's Avatar
 
Join Date: Sep 2005
Location: The Toon (newcastle upon Tyne)
Posts: 8,256
Default

its in javascript

you need to cover the basics in javascript
__________________
Would you like to learn PHP from me? Check out -> www.codezenith.co.uk
davidj is offline   Reply With Quote
Old 02-24-2008, 02:20 PM   #7
moe122
 
Join Date: Feb 2008
Posts: 138
Default

i havent got much time to do that, im already way behind.
moe122 is offline   Reply With Quote
Old 02-24-2008, 02:34 PM   #8
moe122
 
Join Date: Feb 2008
Posts: 138
Default

could you explain how i would do 1 of them, so i get the concept
moe122 is offline   Reply With Quote
Old 02-24-2008, 02:52 PM   #9
davidj
davidj's Avatar
 
Join Date: Sep 2005
Location: The Toon (newcastle upon Tyne)
Posts: 8,256
Default

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
__________________
Would you like to learn PHP from me? Check out -> www.codezenith.co.uk
davidj is offline   Reply With Quote
Old 02-24-2008, 03:31 PM   #10
moe122
 
Join Date: Feb 2008
Posts: 138
Default

so i write the function in js but what do you mean by check it in php?
moe122 is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 10:02 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Copyright 2006 DreamweaverClub.com