-1

I would like to check if one of the input fields is empty and if the #urlink is a the format of a URL before I perform this function in the js:

$scope.favUrls.$add  

not sure how to go about it.

html

<input type="text" id="urlName" class="form-control" placeholder=""  ng-model="mvName" />
<input type="text" id="urlLink" class="form-control" placeholder=""  ng-model="mvUrl" />

app.js

$scope.saveToList = function(event) {

    var mvName = $scope.mvName.trim();
    var mvUrl = $scope.mvUrl.trim();

          if (mvName.length > 0) {
            $scope.favUrls.$add({
              name: mvName,
              title: mvUrl
            });
              urlName.value = ''; //urlName is the ID of  input box - Angular rocks!
            urlLink.value = ''; //urlName is the ID of  input box - Angular rocks!
          }

}
user1937021
  • 10,151
  • 22
  • 81
  • 143
  • Angular has a [built-in](https://docs.angularjs.org/api/ng/input/input%5Burl%5D) for this. – georg Jan 27 '16 at 14:18

1 Answers1

-2

use the following function that validate the URL

function ValidURL(str) {
  var pattern = new RegExp('^(https?:\/\/)?'+ // protocol
    '((([a-z\d]([a-z\d-]*[a-z\d])*)\.)+[a-z]{2,}|'+ // domain name
    '((\d{1,3}\.){3}\d{1,3}))'+ // OR ip (v4) address
    '(\:\d+)?(\/[-a-z\d%_.~+]*)*'+ // port and path
    '(\?[;&a-z\d%_.~+=-]*)?'+ // query string
    '(\#[-a-z\d_]*)?$','i'); // fragment locater
  if(!pattern.test(str)) {
    alert("Please enter a valid URL.");
    return false;
  } else {
    return true;
  }
}

source

Community
  • 1
  • 1
Salah Nour ElDin
  • 510
  • 2
  • 13
  • 1
    Please attribute the code to the original author when copy-pasting from another answer. Or better still, mark this question as a [duplicate](http://stackoverflow.com/questions/5717093/check-if-a-javascript-string-is-an-url) – Turnip Jan 27 '16 at 14:16
  • Thanks i'm sorry but i want to help him to get fast answer – Salah Nour ElDin Jan 27 '16 at 14:18