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.

Link to comment
Share on other sites

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
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.