jQuery: How to check if the content has any URLs

I need to restrict the URL postings in the textarea.

For this I used the code:

    var url_act = jQuery("#area").val();
    var matches = url_act.match(/http:/);

    if (matches)
    {
        alert('You didn\'t have permission to post any url');
        return false;
    }

But if the content has any https: or url starts with www. is not restricted.

How to restrict if the content has any URL formats or not? If the URL is capital letters is not working.

Is there any way to do this?

Answers 1

  • I did not understand your question completely but this should help

    Link to fiddle

    $(document).ready(function() {
      $('#check').click(function() {
        var content = $('#myText').val();
        var pattern = new RegExp("http:");
        var result = '';
    
        result = pattern.test(content) ? 'Invalid' : 'valid';
        alert(result);
      })
    })
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    
    <textarea id="myText" rows="10" cols="50"></textarea>
    <br>
    <button id="check">
      Check
    </button>


Related Articles