0

I'm trying to create a regular expression for matching latitude/longitude coordinates that fall within the correct range. But the below code is giving error. Please let me know where I am going wrong:

Angular:

var app = angular.module('myapp', []);

app.controller('latlong', function($scope) {
  $scope.name = "Valiadtion";
  $scope.lat = "latitude";
  $scope.long = "longitude";

  $scope.submit = function() {
    var lat = $scope.lati;
    var reg = /^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$/;

    if (reg != lat) {
      console.log('error');
    }

  }
});

Here is the Plunker

SRK
  • 163
  • 2
  • 18

1 Answers1

1

Instead of == you must use this

var reg = /^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$/;

var lat = "+90.0, -127.554334";

if (!lat.match(reg)) {
  console.log('error');
} else {
  console.log('passed!');
}

Read more on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match

Saeed
  • 5,413
  • 3
  • 26
  • 40