javascript - How to check if valid facebook url, before submit - Stack Overflow

I am looking for a way to check, if the url a user have posted in my form is a valid facebook url. I am

I am looking for a way to check, if the url a user have posted in my form is a valid facebook url. I am aware of the regx (?:(?:http|https):\/\/)?(?:www.)?facebook\/(?:(?:\w)*#!\/)?(?:pages\/)?(?:[?\w\-]*\/)?(?:profile.php\?id=(?=\d.*))?([\w\-]*)? But how to use it, am i not sure on how to do?

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

How can i make a form validation on that, that checks the facebook regx? Thanks

I am looking for a way to check, if the url a user have posted in my form is a valid facebook url. I am aware of the regx (?:(?:http|https):\/\/)?(?:www.)?facebook.\/(?:(?:\w)*#!\/)?(?:pages\/)?(?:[?\w\-]*\/)?(?:profile.php\?id=(?=\d.*))?([\w\-]*)? But how to use it, am i not sure on how to do?

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

How can i make a form validation on that, that checks the facebook regx? Thanks

Share Improve this question edited Feb 12, 2022 at 16:59 mplungjan 179k28 gold badges182 silver badges240 bronze badges asked May 3, 2016 at 19:59 Jones KingJones King 631 silver badge8 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

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.\/(?:(?:\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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745465026a4628879.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信