1

我正在玩弄验证,一切都在膨胀,除非我不是很喜欢 FILTER_VALIDATE_URL PHP 过滤器,或者我没有正确使用它。这种类型的输入将验证:

www.mysite(通知号 .com)

我想让这个工作:

www.mysite.com

mysite.com

这是我现在使用的代码..

if (empty($web)) {
$webError = '<p class="error">Website Is Required</p>';
}

else if (filter_var($web, FILTER_VALIDATE_URL) === FALSE) {
$webError = '<p class="error">Please Enter A Valid URL</p>';
}
4

1 回答 1

0

Off course, only single error message will be shown, because you each time override the string. And you're approaching it all wrong. You need some kind of error container in order to store them. Then if it has errors, show them in HTML markup. Your code could look like this,

$errors = array();

if (empty($web)) { 
   array_push($errors, 'Website Is Required');

} elseif (filter_var($web, FILTER_VALIDATE_URL) === false) {

   array_push($errors, 'Please Enter A Valid URL');
}

?>

<?php if (!empty($errors)) : ?>
<?php foreach($errors as $error): ?>

<p class="error"><?php echo $error; ?></p>

<?php endforeach; ?>
<?php endif; ?>
于 2013-10-06T05:08:19.133 回答