Log in

View Full Version : Form Validation



calumogg
04-09-2008, 09:37 PM
Hey everyone, I know there is loads of stuff about PHP form validation on the net but I just cant get my head around it.
I am looking for a set of functions that will validate a email field, a purely text field (no numbers or characters), and finally one a field that has text, numbers and characters.

Am I on the right lines with this:

<?php
function textandnumbers($field_name_1)
{
if(!preg_match("/[^a-zA-Z0-9\.\-\Ä\ä\Ö\ö\Ü\ü\]+$/s",$field_name_1)){
$result = valid;
} else {
$result = notvalid;}
}

function text($field_name_2)
{
if(!preg_match("/[^a-zA-Z]+$/s",$field_name_2)){
$result = valid;
} else {
$result = notvalid;}
}

function numbers($field_name_3)
{
if(!preg_match("/[^0-9]+$/ ",$field_name_3)) {
$result = valid;
} else {
$result = notvalid; }
}
?>

Master_script_maker
04-09-2008, 10:21 PM
<?php
function validate($string, $type) {
if($type=="email") {
$valid==eregi('^[0-9a-z]{4,15}[@][0-9a-z]{4,15}[.]([a-z]{2}[.][a-z]{2}|[a-z]{2,4})$', $string)==false?false:true;
}
if($type=="text") {
$valid==eregi('^[a-z]+$', $string)==false?false:true;
}
if($type=="characters") {
$valid==eregi('^[a-zA-Z0-9\.\-\Ä\ä\Ö\ö\Ü\ü]+$', $string)==false?false:true;
}
?>

boogyman
04-10-2008, 12:26 PM
if you wish to continue to use the
PERL Compatible Regular Expressions, just move the carat on the other side of the square bracket. By placing it inside the square bracket you are excluding those terms, when you really want it to be only those terms


preg_match("/^[0-9]+$/ ",$field_name_3)

calumogg
04-10-2008, 05:52 PM
Thanks the replys, its much appreciated.