Jones King Jones King - 2 years ago 99
Javascript Question

How to check if valit facebook url, before submit

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


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 class="col-sm-12">
<button id="SendBtn" class="btn btn-success pull-right">Send</button>

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

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download