define(["../app"], function (app) {
'use strict';
app.directive('sshUrl', function($rootScope){
"use strict";
return {
require: "ngModel",
link: function (scope, element, attr, ngModel) {
if (ngModel) {
var strRegex = /^((https|HTTPS|http|HTTP|ftp|FTP|rtsp|RTSP|mms|MMS)?:\/\/)?(([0-9a-zA-Z_!~*'().&=+$%-]+: )?[0-9a-zA-Z_!~*'().&=+$%-]+@)?((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5]$)|([0-9a-zA-Z_!~*'()-]+\.)*([0-9a-zA-Z][0-9a-zA-Z-]{0,61})?[0-9a-zA-Z]\.[a-zA-Z]{2,6})(:[0-9]{1,4})?((\/?)|(\/[0-9a-zA-Z_!~*'().;?:@&=+$,%#-]+)+\/?)$/;
}
var customerUrlCheck = function (value) {
var validity = ngModel.$isEmpty(value) || strRegex.test(value);
ngModel.$setValidity("sshUrl", validity);
return validity ? value : undefined;
};
ngModel.$formatters.push(customerUrlCheck);
ngModel.$parsers.push(customerUrlCheck);
}
};
});
});
<form ng-submit="" id="adTrackForm" name="adTrackForm" novalidate>
<div class="adtrack_add">
<ul>
<li><span>以下信息必须填写:</span></li>
<li>
<span>目标URL<p>(在其媒体投放广告时所指向的URL)</p></span>
<div class="adtrack_add_input">*
<input ssh-url id="targetUrl" name="targetUrl" ng-model="urlconfig.targetUrl" placeholder="www.mydomain/product.html" required/>
</div>
<span style="color:red" ng-show="adTrackForm.targetUrl.$dirty && adTrackForm.targetUrl.$invalid">
<span ng-show="adTrackForm.targetUrl.$error.required">请输入目标URL</span>
<span ng-show="adTrackForm.targetUrl.$error.sshUrl">您设置的目标URL格式错误</span>
</span>
</li>
</ul>
</div>
<input class="btn btn-info timechange_button" ng-disabled="adTrackForm.targetUrl.$dirty && adTrackForm.targetUrl.$invalid" type="submit" value="提交"/>
</form>