Jump to content

Making shorter work of many IF statement?


rv20

Recommended Posts

I have 3 input fields wich are posted to a php script, now variety of conditions could happen,

 

field 1 could be blank

flield 2 couldbe blank

field 3 could be blank

all 3 could be blank

1 & 2 coulld be blank

2 & 3 blank etc etc

 

or instead of blank they could have disallowed charcters.

 

Now i want to verify with php and not js, so i could make many IF / ELSEIF statements and build a final string that is send back, but this is a lot of code and messy is there a better shorter way of doing it?

 

I could post back and say, there are errors please try again but i want to point out an exact list of all errors which means checking everthing.

try something like this:

 

<?php
$fields = array('fname','lname','phone');
$valids = array("/^[a-z'-]+$/i","/^[a-z'-]+$/i","/^1?\s?[\.\)]?\s?\d{3}\s?[\.-]?\s?\d{3}\s?[\.-]?\s?\d{4}$/");
foreach ($fields as $k => $v) {
if ($_POST[$v]) {
	if (!preg_match($valids[$k],trim($_POST[$v]))) {
		// error, field doesn't pass validation
		break;
	}
} else {
	// error, field isn't filled in
	break;
}
// field passes validations
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.