duanbichou4942 2013-10-06 04:40
浏览 34

验证没有FILTER_VALIDATE_URL的URL

I am toying with validations and all is going swell except I am not really liking the FILTER_VALIDATE_URL PHP filter, or I am not using it correctly. This type of input will validate:

www.mysite (notice no .com)

I would like for this to work:

www.mysite.com

mysite.com

Here is the code I am using now..

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>';
}
  • 写回答

1条回答 默认 最新

  • doubo4336 2013-10-06 05:08
    关注

    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; ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?