Commit 9769e65a

mo khan <mo@mokhan.ca>
2014-10-04 04:35:21
add client side url validation.
1 parent c6d374d
Changed files (2)
app
assets
javascripts
backbone
models
templates
app/assets/javascripts/backbone/models/profile.js.coffee
@@ -16,6 +16,16 @@ class CakeSide.Models.Profile extends Backbone.Model
   validate: (attributes, options) ->
     return "Name can't be blank" unless attributes.name && attributes.name.trim()
     return "Email can't be blank" unless attributes.email && attributes.email.trim()
+    return "Email is invalid" unless @validateEmail(attributes.email)
+    return "URL is invalid" if attributes.website && !@validateUrl(attributes.website)
+
+  validateEmail: (email) ->
+    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,}))$/
+    regex.test(email)
+
+  validateUrl: (url) ->
+    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")
+    regex.test(url)
 
 class CakeSide.Collections.ProfilesCollection extends Backbone.Collection
   model: CakeSide.Models.Profile
app/assets/javascripts/backbone/templates/profiles/show.jst.ejs
@@ -20,16 +20,16 @@
       </div>
     </div>
     <div class="control-group">
-      <label class="control-label" for="user_city">City</label>
+      <label class="control-label" for="user_website">URL</label> 
       <div class="controls"> 
-        <input class="input-xlarge" id="user_city" name="user[city]" type="text" value="<%= city %>">
+        <input class="input-xlarge url" id="user_website" name="user[website]" placeholder="https://www.cakeside.com" type="url" value="<%= website %>">
         <span class="help-inline"></span>
       </div>
     </div>
     <div class="control-group">
-      <label class="control-label" for="user_website">Website</label> 
+      <label class="control-label" for="user_city">Location</label>
       <div class="controls"> 
-        <input class="input-xlarge url" id="user_website" name="user[website]" placeholder="https://www.cakeside.com" type="url" value="<%= website %>">
+        <input class="input-xlarge" id="user_city" name="user[city]" type="text" value="<%= city %>">
         <span class="help-inline"></span>
       </div>
     </div>