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+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/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+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/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?