Q:

AngularJS: prepend "http://" to ng-model

I have an input for url. I would like to automatically prepend "http://" to the user value, if it does not already exist. I would like to avoid using $watch because there are about 30 such inputs in a single controller...
How should I approach this?

A:

I would say with a classic regex on the blur event you should be fine:
<div ng-blur="check(thismodel)"></div>

#
$scope.check = function($url){
  if (!/^(?:(ftp|http|https)?:\/\/)?(?:[\w-]+\.)+([a-z]|[A-Z]|[0-9]){2,6}$/gi.test($url)) {
      $url = "http://" + $url;
  }
}

