在提交之前如何检查valit facebook url

时间:2016-05-03 19:59:51

标签: javascript jquery

我正在寻找一种方法来检查,如果用户在我的表单中发布的网址是有效的facebook网址。我知道regx (?:(?:http|https):\/\/)?(?:www.)?facebook.com\/(?:(?:\w)*#!\/)?(?:pages\/)?(?:[?\w\-]*\/)?(?:profile.php\?id=(?=\d.*))?([\w\-]*)? 但是如何使用它,我不知道该怎么做?

<div class="col-sm-6">
    <div class="col-sm-12">
        <b>Facebook Url</b>
        <input id="FacebookUrl" name="FacebookUrl" type="text" class="form-control" />
    </div>

    <div class="col-sm-12">
        <button id="SendBtn" class="btn btn-success pull-right">Send</button>
    </div>
</div>

我如何进行表单验证,检查facebook regx?感谢

1 个答案:

答案 0 :(得分:1)

In the button onclick event:

<button id="SendBtn" class="btn btn-success pull-right" onclick="return validateUrl();">Send</button>

And then you can open a script tag in the same file or other file and implement your regex.

function validateUrl() {
  url = $("#FacebookUrl").val();
  var pattern = /^(?:(?:http|https):\/\/)?(?:www.)?facebook.com\/(?:(?:\w)*#!\/)?(?:pages\/)?(?:[?\w\-]*\/)?(?:profile.php\?id=(?=\d.*))?([\w\-]*)?$/;
  if(pattern.test(url)) {
    alert("Correct URL");
  }
  else {
    alert("Incorrect URL");
  }
  return pattern.test(url);
}

And with this result prevent the action.