Page 1 of 1

Validation

Posted: Sat May 28, 2005 1:12 pm
by isabelle
I am trying to construct a simple form where users can enter their information. I am having an issue with the field "VIN" on this page http://www.yokokumano.com/ducati/ch08/registration.php

When there is no data in the field, an error message stops the entry from being entered into the database. But I set the parameters for 14 number digits only and even if I put the wrong amount of digits, it will enter it into the database.

Code: Select all

if(isset($submit_button)){

	if(trim($vin)=='') {
		$error_msg.="Please enter a VIN<br>";
	} else {
		// check if vin is 14 digits
		if(!ereg("[0-9]{14}", $vin)) $error_msg.="Please enter a valid VIN #<br>";
	}

	if(trim($email)=='') {
		$error_msg.="Please enter an email<br>";
	} else {
		// check if email is a valid address in this format username@domain.com
		if(!ereg("[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]", $email)) $error_msg.="Please enter a valid email address<br>";
	}
	
	// display error message if any, if not, proceed to other processing
	if($error_msg==''){
		// other process here
	} else {
		echo "<font color=red>$error_msg</font>";
	}

}
if (!empty($_GET["surname"]) &&
    !empty($_GET["firstname"]) &&
    !empty($_GET["email"]) &&
    !empty($_GET["phone"]) &&
    !empty($_GET["vin"]))

Posted: Sat May 28, 2005 3:22 pm
by JAM
I must have missed something, but it works here...

Code: Select all

// bad
    if(trim($vin)=='') {
        $error_msg.="Please enter a VIN<br>";
    } else {
        // check if vin is 14 digits
        if(!ereg("[0-9]{14}", $vin)) $error_msg.="Please enter a valid VIN #<br>";
    }

// better
    if(trim($vin)=='') {
        $error_msg.="Please enter a VIN<br>";
    } elseif(!ereg("[0-9]{14}", $vin)) {
        $error_msg.="Please enter a valid VIN #<br>";
    }
Might just be some error in the form of writing?

You could also verify with

Code: Select all

if (!is_numeric($vin) or strlen($vin) <> 14) { echo 'bad'; }
    else { echo 'ok'; }
...although regexp is the way to go. Sometimes looking at it in different ways might clear up the issues...