master
 1class csx.Models.Profile extends Backbone.Model
 2  paramRoot: 'profile'
 3  urlRoot: '/api/v1/profiles'
 4  modelEvents:
 5    "change": "render"
 6
 7  defaults:
 8    id: null
 9    name: null
10    email: null
11    city: null
12    website: null
13    facebook: null
14    twitter: null
15
16  validate: (attributes, options) ->
17    return "Name can't be blank" unless attributes.name && attributes.name.trim()
18    return "Email can't be blank" unless attributes.email && attributes.email.trim()
19    return "Email is invalid" unless @validateEmail(attributes.email)
20    return "URL is invalid" if attributes.website && !@validateUrl(attributes.website)
21    return "Twitter handle is invalid" if !@validateTwitter(attributes.twitter)
22
23  validateEmail: (email) ->
24    regex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
25    regex.test(email)
26
27  validateUrl: (url) ->
28    regex = new RegExp( "^" + "(?:(?:https?|ftp)://)" + "(?:\\S+(?::\\S*)?@)?" + "(?:" + "(?!(?:10|127)(?:\\.\\d{1,3}){3})" + "(?!(?:169\\.254|192\\.168)(?:\\.\\d{1,3}){2})" + "(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})" + "(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" + "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" + "(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" + "|" + "(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)" + "(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*" + "(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))" + ")" + "(?::\\d{2,5})?" + "(?:/\\S*)?" + "$", "i")
29    regex.test(url)
30
31  validateTwitter: (twitter) ->
32    return true unless twitter
33    regex = /^@?(\w){1,15}$/
34    regex.test(twitter)