1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-04 15:24:23 +01:00
fab-manager/app/assets/javascripts/directives/validators.coffee
2016-03-23 18:39:41 +01:00

34 lines
896 B
CoffeeScript

'use strict'
Application.Directives.directive 'url', [ ->
URL_REGEXP = /^(https?:\/\/)([\da-z\.-]+)\.([-a-z0-9\.]{2,30})([\/\w \.-]*)*\/?$/
{
require: 'ngModel'
link: (scope, element, attributes, ctrl) ->
ctrl.$validators.url = (modelValue, viewValue) ->
if ctrl.$isEmpty(modelValue)
return true
if URL_REGEXP.test(viewValue)
return true
# otherwise, this is invalid
return false
}
]
Application.Directives.directive 'endpoint', [ ->
ENDPOINT_REGEXP = /^\/([-._~:?#\[\]@!$&'()*+,;=%\w]+\/?)*$/
{
require: 'ngModel'
link: (scope, element, attributes, ctrl) ->
ctrl.$validators.endpoint = (modelValue, viewValue) ->
if ctrl.$isEmpty(modelValue)
return true
if ENDPOINT_REGEXP.test(viewValue)
return true
# otherwise, this is invalid
return false
}
]