0

HTML

<table class="table table-bordered table-striped table-responsive" id="tableid">

  <thead>
    <tr>

      <th class="other-col">ID</th>
      <th>URL</th>

    </tr>
  </thead>
  <!--Table Rows-->
  <tbody id="table-body">
    <tr id="td" <td id="table_id" name="id" contenteditable='true' class="other-col">1</td>

      <td id="table_url" name="url" contenteditable='true'>https://fb.com</td>
    </tr>

    <tr id="td">

      <td id="table_id" name="id" contenteditable='true' class="other-col">2</td>
      <td id="table_url" name="url" contenteditable='true'>http://twitter.com/</td>
    </tr>

    <tr id="td">

      <td id="table_id" name="id" contenteditable='true' class="other-col">3</td>
      <td id="table_url" name="url" contenteditable='true'>htt://google.com/</td>
    </tr>

  </tbody>
  <!--Table Rows-->
</table>

<!--Button Section-->
<div class="buttons-section">
  <!--Button-->
  <button type="button" id="valid" class="btn btn-default custom-btn">VALIDATE</button>
  <!--End Buttons-->
</div>
<!--End Buttons Section-->

JavaScript

$(document).ready(function() {
    var patt = /^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/i;
    $('button').click(function() {
        var checked = $('input:checked');
        $.each(checked, function(i, val) {
            var td = $(val).parents('tr').find('#table_url');
            var text = td.text();
            if (!patt.test(text)) { // Pattern to check if url is valid 
                $(td).css('color', 'red');
            } else {
                $(td).css('color', 'green');
            }
        });
    });
});
Mikey
  • 6,728
  • 4
  • 22
  • 45
jaan
  • 1
  • It is not clear which part of the problem you are stuck on. If you are simply looking for a method of validating URLs in JavaScript, this is probably the goto StackOverflow thread: https://stackoverflow.com/questions/5717093/check-if-a-javascript-string-is-a-url . It would be useful if you could show your attempt to solve the problem then somebody will be able to give you more specific help. – Turnip May 09 '18 at 13:34
  • Could you please describe your problem a little more? What would you like to validate? Would you like to check if url is correct? Or does url exists? What would you like to achieve? – Krzysztof Janiszewski May 09 '18 at 13:37
  • Before posting a question, give a brief explanation about what exactly you are doing, what needs to be done and what is being done. – Krishna Prashatt May 09 '18 at 13:37
  • Possible duplicate of [Check if a Javascript string is a url](https://stackoverflow.com/questions/5717093/check-if-a-javascript-string-is-a-url) – Krzysztof Janiszewski May 09 '18 at 13:38
  • $(document).ready(function() { var patt = /^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/i; $('button').click(function() { var checked = $('input:checked'); $.each(checked, function(i, val) { var td = $(val).parents('tr').find('#table_url'); var text = td.text(); if (!patt.test(text)) { // Pattern to check if url is valid $(td).css('color', 'red'); } else { $(td).css('color', 'green'); } }); }); }); – jaan May 09 '18 at 13:38
  • i want to do when the 'validate' button is clicked then url column data is checked with a pattern, if it is valid then its color turns green if invalid then red.. – jaan May 09 '18 at 13:41
  • thanks very much to all for asking.. plz help me out – jaan May 09 '18 at 13:42
  • Why is there a mention of `input:checked` in your JS code but no `` tags in your actual HTML code? Update your post with what you are trying to do and ensure your HTML has all the relevant code. – Mikey May 09 '18 at 14:08

0 Answers0