Skip to content

Commit d5457b5

Browse files
committed
Validation for no-spaces, a-zA-Z, emails added
1 parent f65b8c6 commit d5457b5

File tree

4 files changed

+80
-10
lines changed

4 files changed

+80
-10
lines changed

web/js/html.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ App.HTML.Build.user_form = function(options, id)
220220
tpl.set(':save_button', 'SAVE');
221221
}
222222

223-
options = !App.Helpers.isEmpty(options) ? options : {'CONTACT':'', 'PASSWORD':'','LOGIN_NAME':'','NS':''};
223+
options = !App.Helpers.isEmpty(options) ? options : {'CONTACT':'', 'PASSWORD':'','LOGIN_NAME':'','LNAME':'', 'FNAME':''};
224224

225225
tpl = App.HTML.setTplKeys(tpl, options, true);
226226
tpl = App.HTML.Build.user_selects(tpl, options);

web/js/model.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,11 @@ App.Model.add = function(values, source_json)
7272
App.Helpers.Warn('Changes were not applied');
7373
}
7474
else {
75-
var build_method = App.Env.getWorldName() + '_entry';
75+
/*var build_method = App.Env.getWorldName() + '_entry';
7676
var tpl = App.HTML.Build[build_method](values, 'new');
77-
App.Ref.CONTENT..replaceWith(tpl);
77+
App.Ref.CONTENT..replaceWith(tpl);*/
7878
// todo: reply.data;
79+
App.Pages.prepareHTML();
7980
}
8081
});
8182
}

web/js/templates.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,11 @@ App.Templates.html = {
285285
</div>\
286286
<div class="form-row cc">\
287287
<label for="#" class="field-label">username:</label>\
288-
<input type="text" class="text-field" value="~!:LOGIN_NAME~!" name="LOGIN_NAME">\
288+
<input type="text" class="text-field rule-abc rule-required" value="~!:LOGIN_NAME~!" name="LOGIN_NAME">\
289289
</div>\
290290
<div class="form-row pwd-box cc">\
291291
<label for="#" class="field-label">password:</label>\
292-
<input type="text" class="text-field password" name="PASSWORD" value="~!:PASSWORD~!">\
292+
<input type="text" class="text-field password rule-required" name="PASSWORD" value="~!:PASSWORD~!">\
293293
<span class="generate-pwd do_action_generate_pass">Generate</span>\
294294
</div>\
295295
<div class="form-row cc">\
@@ -298,7 +298,7 @@ App.Templates.html = {
298298
~!:PACKAGE_OPTIONS~!\
299299
</select>\
300300
</div>\
301-
<div class="form-row cc">\
301+
<div class="form-row cc hidden">\
302302
<label for="#" class="field-label">shell:</label>\
303303
<select class="not-styled" name="SHELL">\
304304
~!:SHELL_OPTIONS~!\
@@ -312,13 +312,21 @@ App.Templates.html = {
312312
</div>\
313313
<div class="form-row cc">\
314314
<label for="#" class="field-label">contact email:</label>\
315-
<input type="text" name="CONTACT" class="text-field" value="~!:CONTACT~!">\
315+
<input type="text" name="CONTACT" class="text-field rule-email rule-required" value="~!:CONTACT~!">\
316316
</div>\
317317
<div class="form-row cc">\
318318
<label for="#" class="field-label">reports:</label>\
319319
<input type="checkbox" name="REPORTS_ENABLED" class="not-styled" value="~!:REPORTS_ENABLED~!">\
320320
</div>\
321+
<div class="form-row cc">\
322+
<label for="#" class="field-label">Firstname:</label>\
323+
<input type="text" name="FNAME" class="not-styled rule-abc rule-required" value="~!:FNAME~!">\
324+
</div>\
321325
<div class="form-row cc">\
326+
<label for="#" class="field-label">Lastname:</label>\
327+
<input type="text" name="LNAME" class="not-styled rule-abc rule-required" value="~!:LNAME~!">\
328+
</div>\
329+
<div class="form-row cc hidden">\
322330
<label for="#" class="field-label">ns1:</label>\
323331
<input type="text" name="NS" class="text-field" value="~!:NS~!">\
324332
</div>\
@@ -357,6 +365,7 @@ App.Templates.html = {
357365
<div class="username-box">\
358366
<span class="user">\
359367
<span class="nickname do_action_edit">~!:LOGIN_NAME~!</span>\
368+
<span class="role">~!:FNAME~! ~!:LNAME~!</span>\
360369
<span class="role">(~!:ROLE~!)</span>\
361370
</span>\
362371
<span class="prop-box template-box">\

web/js/validators.js

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,39 @@ App.Validate.Is = {
1313
}
1414
};
1515

16+
App.Validate.getFieldName = function(elm)
17+
{
18+
fb.log(elm);
19+
fb.warn($(elm).prev('label').text());
20+
return ['<strong>', $(elm).prev('label').text(), '</strong>'].join('');
21+
}
22+
23+
App.Validate.Rule = {
24+
'required' : function(elm) {
25+
if ($(elm).val().trim() == '') {
26+
return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' is required'};
27+
}
28+
return {VALID: true};
29+
},
30+
'no-spaces': function(elm) {
31+
if ($(elm).val().search(/\s/) != -1) {
32+
return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' cannot contain spaces'};
33+
}
34+
return {VALID: true};
35+
},
36+
'abc': function(elm) {
37+
if ($(elm).val().search(/[^a-zA-Z]+/) != -1) {
38+
return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' must contain only letters'};
39+
}
40+
return {VALID: true};
41+
},
42+
'email': function(elm) {
43+
if ($(elm).val().search(/^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/) == -1) {
44+
return {VALID: false, ERROR: App.Validate.getFieldName(elm) + ' not a valid email'};
45+
}
46+
return {VALID: true};
47+
}
48+
}
1649

1750

1851
App.Validate.form = function(world, elm)
@@ -25,11 +58,23 @@ App.Validate.form = function(world, elm)
2558
//return; // pass
2659
}
2760
else {
28-
29-
if ($(field).val().trim() == '' || $(field).val().trim() == '-') {
61+
var rules = App.Validate.getRules(field);
62+
$(rules).each(function(i, rule)
63+
{
64+
fb.log('Validate with %o %o', rule, field);
65+
if (App.Validate.Rule[rule]) {
66+
var result = App.Validate.Rule[rule](field);
67+
fb.log(result);
68+
if (result.VALID == false) {
69+
App.Env.FormError.push(result.ERROR); //$(field).attr('name') + ' is required');
70+
form_valid = false;
71+
}
72+
}
73+
})
74+
/*if ($(field).val().trim() == '' || $(field).val().trim() == '-') {
3075
App.Env.FormError.push($(field).attr('name') + ' is required');
3176
form_valid = false;
32-
}
77+
}*/
3378
}
3479
});
3580
return form_valid;
@@ -49,5 +94,20 @@ App.Validate.displayFormErrors = function(world, elm)
4994
ref.html(errors_tpl);
5095
}
5196

97+
App.Validate.getRules = function(elm)
98+
{
99+
var rules_string = $(elm).attr('class');
100+
var rules = [];
101+
$(rules_string.split(/\s/)).each(function(i, str)
102+
{
103+
var rule = str.split('rule-');
104+
if (rule.length > 1) {
105+
rules[rules.length++] = rule[1];
106+
}
107+
});
108+
109+
return rules;
110+
}
111+
52112

53113

0 commit comments

Comments
 (0)