1

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?

blurfus
  • 13,485
  • 8
  • 55
  • 61
Sam Hanson
  • 1,317
  • 4
  • 22
  • 48
  • 1
    http://stackoverflow.com/questions/3809401/what-is-a-good-regular-expression-to-match-a-url – Shiva Apr 06 '16 at 06:05

3 Answers3

4

Change your regex to,

var matches = url_act.match(/https?:|\bwww\./i);

i modifier helps to do a case-insensitive match.

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • Thanks. Its working fine. If we want to restrict any 2 or 3 particular domain how do we change the code. i.e If I want to restrict urls from youtube and soundcloud. If so how to do this. – Sam Hanson Apr 06 '16 at 06:18
  • 1
    simple, `url.match(/^https:\/\/www\.(?:soundcloud|youtube)\.com\b/i);` – Avinash Raj Apr 06 '16 at 06:20
  • One more request please. If we want to restrict other than this youtube or soundcloud how to change this. i.e I can post any raw content or content with youtube or soundcloud urls. But If I post content with any other domains like google or vimoeo or any thing how to restrict this. – Sam Hanson Apr 06 '16 at 06:50
  • try `url.match(/^(?!https:\/\/www\.(?:soundcloud|youtube)\.com\b).+/i);` – Avinash Raj Apr 06 '16 at 07:06
0

try the following regex. It may help you.

http://www.regextester.com/20

Tran Ho
  • 1,442
  • 9
  • 15
0

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>
Angad
  • 69
  • 3