验证URL的长度,大写字母和某些整个单词?

时间:2012-05-22 17:50:45

标签: php javascript regex

我有一些代码可以获取用户在注册我的应用时选择的网址并进行检查。但是我想对它添加以下限制。

  • 必须至少3个字母
  • 没有国会大厦字母
  • 没有一句话“管理,支持等......”

这是我到目前为止所拥有的 - 但它现在只寻找特殊字母:

elseif($action == "validate"){

    $option = $_GET["option"];

    $value = $_GET["value"];

    switch($option){

    case "URL":             

$sql = mysql_select_db($value,$connect);
if(preg_match ( "@[^A-Za-z0-9]+@i", $value ) > 0) { $valid = "FALSE";   continue; }

$sql == 1 ?  $valid = "FALSE" : $valid = "TRUE";

}

2 个答案:

答案 0 :(得分:1)

if(preg_match("@^[a-z0-9]{3,25}$@", $value ) && (!preg_match("@admin|support|anything else@",$value))) { 
    //Restrict the word length to 25 characters at max, 3 at least.
    //Do some stuff. The $value meets the requirements as set forth
 ...
}

答案 1 :(得分:0)

elseif($action == "validate"){

    $option = $_GET["option"];

    $value = $_GET["value"];

    switch($option){

    case "URL":             

    $sql = mysql_select_db($value,$connect);
    if(preg_match ( "@[^A-Za-z0-9]+@i", $value ) > 0) { $valid = "FALSE";   continue; }
    if(strlen($value) <= 3) { $valid = "FALSE";   continue; } //less than 3 char
    if(strstr($value, 'admin')) { $valid = "FALSE";   continue; }
      //.. the same for your other words

    $sql == 1 ?  $valid = "FALSE" : $valid = "TRUE";

}