Browse Source

Validation for no-spaces, a-zA-Z, emails added

Dima Malishev 14 năm trước cách đây
mục cha
commit
d5457b58d0
4 tập tin đã thay đổi với 80 bổ sung10 xóa
  1. 1 1
      web/js/html.js
  2. 3 2
      web/js/model.js
  3. 13 4
      web/js/templates.js
  4. 63 3
      web/js/validators.js

+ 1 - 1
web/js/html.js

@@ -220,7 +220,7 @@ App.HTML.Build.user_form = function(options, id)
         tpl.set(':save_button', 'SAVE'); 
     }
     
-    options = !App.Helpers.isEmpty(options) ? options : {'CONTACT':'', 'PASSWORD':'','LOGIN_NAME':'','NS':''};
+    options = !App.Helpers.isEmpty(options) ? options : {'CONTACT':'', 'PASSWORD':'','LOGIN_NAME':'','LNAME':'', 'FNAME':''};
     
     tpl = App.HTML.setTplKeys(tpl, options, true);        
     tpl = App.HTML.Build.user_selects(tpl, options);

+ 3 - 2
web/js/model.js

@@ -72,10 +72,11 @@ App.Model.add = function(values, source_json)
             App.Helpers.Warn('Changes were not applied');
         }
         else {
-            var build_method = App.Env.getWorldName() + '_entry';
+            /*var build_method = App.Env.getWorldName() + '_entry';
             var tpl = App.HTML.Build[build_method](values, 'new');
-            App.Ref.CONTENT..replaceWith(tpl);
+            App.Ref.CONTENT..replaceWith(tpl);*/
             // todo: reply.data;
+            App.Pages.prepareHTML();
         }
     });
 }

+ 13 - 4
web/js/templates.js

@@ -285,11 +285,11 @@ App.Templates.html = {
 						</div>\
 						<div class="form-row cc">\
 							<label for="#" class="field-label">username:</label>\
-							<input type="text" class="text-field" value="~!:LOGIN_NAME~!" name="LOGIN_NAME">\
+							<input type="text" class="text-field rule-abc rule-required" value="~!:LOGIN_NAME~!" name="LOGIN_NAME">\
 						</div>\
 						<div class="form-row pwd-box cc">\
 							<label for="#" class="field-label">password:</label>\
-							<input type="text" class="text-field password" name="PASSWORD" value="~!:PASSWORD~!">\
+							<input type="text" class="text-field password rule-required" name="PASSWORD" value="~!:PASSWORD~!">\
 							<span class="generate-pwd do_action_generate_pass">Generate</span>\
 						</div>\
 						<div class="form-row cc">\
@@ -298,7 +298,7 @@ App.Templates.html = {
 								~!:PACKAGE_OPTIONS~!\
 							</select>\
 						</div>\
-                        <div class="form-row cc">\
+                        <div class="form-row cc hidden">\
 							<label for="#" class="field-label">shell:</label>\
 							<select class="not-styled" name="SHELL">\
 								~!:SHELL_OPTIONS~!\
@@ -312,13 +312,21 @@ App.Templates.html = {
 						</div>\
 						<div class="form-row cc">\
 							<label for="#" class="field-label">contact email:</label>\
-							<input type="text" name="CONTACT" class="text-field" value="~!:CONTACT~!">\
+							<input type="text" name="CONTACT" class="text-field rule-email rule-required" value="~!:CONTACT~!">\
 						</div>\
 						<div class="form-row cc">\
 							<label for="#" class="field-label">reports:</label>\
 							<input type="checkbox" name="REPORTS_ENABLED" class="not-styled" value="~!:REPORTS_ENABLED~!">\
 						</div>\
+                        <div class="form-row cc">\
+							<label for="#" class="field-label">Firstname:</label>\
+							<input type="text" name="FNAME" class="not-styled rule-abc  rule-required" value="~!:FNAME~!">\
+						</div>\
 						<div class="form-row cc">\
+							<label for="#" class="field-label">Lastname:</label>\
+							<input type="text" name="LNAME" class="not-styled rule-abc rule-required" value="~!:LNAME~!">\
+						</div>\
+                        <div class="form-row cc hidden">\
 							<label for="#" class="field-label">ns1:</label>\
 							<input type="text" name="NS" class="text-field" value="~!:NS~!">\
 						</div>\
@@ -357,6 +365,7 @@ App.Templates.html = {
 									<div class="username-box">\
 										<span class="user">\
 											<span class="nickname do_action_edit">~!:LOGIN_NAME~!</span>\
+                                            <span class="role">~!:FNAME~! ~!:LNAME~!</span>\
 											<span class="role">(~!:ROLE~!)</span>\
 										</span>\
 										<span class="prop-box template-box">\

+ 63 - 3
web/js/validators.js

@@ -13,6 +13,39 @@ App.Validate.Is = {
     }
 };
 
+App.Validate.getFieldName = function(elm)
+{
+    fb.log(elm);
+    fb.warn($(elm).prev('label').text());
+    return ['<strong>', $(elm).prev('label').text(), '</strong>'].join('');
+}
+
+App.Validate.Rule = {
+    'required' : function(elm) {
+        if ($(elm).val().trim() == '') {
+            return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' is required'};
+        }
+        return {VALID: true};
+    },
+    'no-spaces': function(elm) {
+        if ($(elm).val().search(/\s/) != -1) {
+            return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' cannot contain spaces'};
+        }
+        return {VALID: true};
+    },
+    'abc':      function(elm) {
+        if ($(elm).val().search(/[^a-zA-Z]+/) != -1) {
+            return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' must contain only letters'};
+        }
+        return {VALID: true};
+    },
+    'email':      function(elm) {
+        if ($(elm).val().search(/^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/) == -1) {
+            return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' not a valid email'};
+        }
+        return {VALID: true};
+    }
+}
 
 
 App.Validate.form = function(world, elm)
@@ -25,11 +58,23 @@ App.Validate.form = function(world, elm)
             //return; // pass            
         }
         else {
-        
-            if ($(field).val().trim() == '' || $(field).val().trim() == '-') {
+            var rules = App.Validate.getRules(field);
+            $(rules).each(function(i, rule)
+            {
+                fb.log('Validate with %o %o', rule, field);
+                if (App.Validate.Rule[rule]) {
+                    var result = App.Validate.Rule[rule](field);
+                    fb.log(result);
+                    if (result.VALID == false) {
+                        App.Env.FormError.push(result.ERROR); //$(field).attr('name') + ' is required');
+                        form_valid = false;
+                    }
+                }
+            })
+            /*if ($(field).val().trim() == '' || $(field).val().trim() == '-') {
                 App.Env.FormError.push($(field).attr('name') + ' is required');
                 form_valid = false;
-            }
+            }*/
         }
     });
     return form_valid;
@@ -49,5 +94,20 @@ App.Validate.displayFormErrors = function(world, elm)
     ref.html(errors_tpl);
 }
 
+App.Validate.getRules = function(elm)
+{
+    var rules_string = $(elm).attr('class');
+    var rules = [];
+    $(rules_string.split(/\s/)).each(function(i, str)
+    {        
+        var rule = str.split('rule-');
+        if (rule.length > 1) {
+            rules[rules.length++] = rule[1];
+        }
+    });
+    
+    return rules;
+}
+