PDA

View Full Version : Form Validation


lux
02-19-2007, 02:23 PM
Hey,

Im looking for some advise on php form validation.

What would people recommend: php v Javascript form validation

and maybe some helpful examples if thats cool

regards

Lux

davidj
02-19-2007, 06:12 PM
it depends

i use JS to determin string lengths and date formats are correct i then have to sometimes do db lookups and some compare validation. for example You might have masks in your db like bank account numbers and sort codes to locate bank branches or you might have read in a employee file and then do man lookups.

you may want to use the add_slashes() and strip_slashes() function where textfields are being passed as a simple > ' < could kill your app. (the above functions add \ infront to make any > ' < literal so you could say that you are validating by using those functions

so it depends on what you want validating.

may use a bit of both.

give an example of ya data and ill point you in the right direction

lux
02-20-2007, 08:26 AM
cool,

well the majority of fields will be cover a variety of text. The only real concern is that there is text in the fields.

I've got a couple of fields where i'm storing ip addresses so i'll need a way to ensure that the input is correct as well as having to store email addresses.

you got an example davidj? cheers boss

davidj
02-20-2007, 09:03 AM
well the majority of fields will be cover a variety of text. The only real concern is that there is text in the fields.


a simple Javascript validation script. Let the function perform the submit not the form. Notice my submit button is a plain button not a submit button

<script language="javascript" type="text/javascript">

function checkFlds(){

f1 = document.getElementById('field1'); // form fields
f2 = document.getElementById('field2'); // form fields
f3 = document.getElementById('field3'); // form fields

if (f1.value && f2.value && f3.value){

document.forms[0].submit();

}else{

alert("one of the values are empty");

}
}
</script>



<form id="form1" name="form1" method="post" action="">
<p>
<input type="text" name="textfield" />
</p>
<p>
<input type="text" name="textfield2" />
</p>
<p>
<input type="text" name="textfield3" />
</p>
<p>
<input type="text" name="textfield4" />
</p>
<p>
<input type="button" name="Button" value="Button" onclick="checkFlds()"/>
</p>
</form>

lux
02-20-2007, 09:05 AM
cool, cheers boss