1

I have a very weird issue with my code.

I have a form builder that renders input based on input type. I have added a URL type but I cannot seem to pass a custom URL-pattern regex via the input field.

// In javascript, I access the custom `url-pattern` like:

$(function() {
  if ($('input.url_type_platform').length) {
    $('.url_type_platform').keyup(function() {
      var $th = $(this);
      if (isValidUrl($th.val(), $th.attr('url-pattern')) === 1) {
        alert("got a valid url!");
      }
    });
  }
});

function isValidUrl(url, pattern) {
  var myVariable = url;
  if (pattern.test(myVariable)) { // throws an error, TypeError: pattern.test is not a function
    return 1;
  } else {
    return -1;
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<input url-pattern="/\b(?:(?:https?):\/\/|www\.)[-a-z0-9+&amp;@#\/%?=~_|!:,.;]*[-a-z0-9+&amp;@#\/%=~_|]/i" placeholder="" class="form-control url_type_platform" type="text" id="twitter_link" value="https://twitter.com/quickpublisher">

The issue is that when i throw in the PHP variable it works:

 var pattern = <?php echo platform::getDefaultUrlPatternRegex(); ?>
// \b(?:(?:https?):\/\/|www\.)[-a-z0-9+&amp;@#\/%?=~_|!:,.;]*[-a-z0-9+&amp;@#\/%=~_|]/i

Both console.log(<?php echo platform::getDefaultUrlPatternRegex(); ?>); and console.log(pattern); give the same result. But the latter still throws a

TypeError: pattern.test is not a function

Could there be an extra quote?

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Rotimi
  • 4,783
  • 4
  • 18
  • 27

0 Answers0