mpog-user-validations.js
4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
(function(){
function set_initial_form_custom_data() {
jQuery('#profile_data_country').val("BR");
jQuery("#password-balloon").html(jQuery("#user_password_menssage").val());
}
function check_reactivate_account(value, input_object){
jQuery.ajax({
url : "/plugin/mpog_software/check_reactivate_account",
type: "GET",
data: { "email": value },
success: function(response) {
if( jQuery("#forgot_link").length == 0 )
jQuery(input_object).parent().append(response);
else
jQuery("#forgot_link").html(response);
},
error: function(type, err, message) {
console.log(type+" -- "+err+" -- "+message);
}
});
}
function put_brazil_based_on_email(){
var suffixes = ['gov.br', 'jus.br', 'leg.br', 'mp.br'];
var value = this.value;
var input_object = this;
var gov_suffix = false;
suffixes.each(function(suffix){
var has_suffix = new RegExp("(.*)"+suffix+"$", "i");
if( has_suffix.test(value) ) {
gov_suffix = true;
jQuery("#profile_data_country").val("BR");
}
});
jQuery("#profile_data_country").find(':not(:selected)').css('display', (gov_suffix?'none':'block'));
check_reactivate_account(value, input_object)
}
function validate_email_format(){
var correct_format_regex = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/;
if( this.value.length > 0 ) {
if(correct_format_regex.test(this.value))
this.className = "validated";
else
this.className = "invalid";
} else
this.className = "";
}
function verify_user_password_size() {
if( this.value.length < 6 ) {
jQuery(this).switchClass("validated", "invalid");
} else {
jQuery(this).switchClass("invalid", "validated");
}
}
function show_or_hide_phone_mask() {
if(jQuery("#profile_data_country").val() == "BR") {
if( (typeof jQuery("#profile_data_cell_phone").data("rawMaskFn") === 'undefined') ) {
jQuery("#profile_data_cell_phone").mask("(99) 9999?9-9999");
jQuery("#profile_data_comercial_phone").mask("(99) 9999?9-9999");
}
} else {
jQuery("#profile_data_cell_phone").unmask();
jQuery("#profile_data_comercial_phone").unmask();
}
}
function fix_phone_mask_format(id) {
jQuery(id).blur(function() {
var last = jQuery(this).val().substr( jQuery(this).val().indexOf("-") + 1 );
if( last.length == 3 ) {
var move = jQuery(this).val().substr( jQuery(this).val().indexOf("-") - 1, 1 );
var lastfour = move + last;
var first = jQuery(this).val().substr( 0, 9 );
jQuery(this).val( first + '-' + lastfour );
}
});
}
function set_full_name_validation() {
function is_invalid_formated(text) {
var reg_firsts_char = /(^|\s)([a-z]|[0-9])/g;
var reg_special_char = /[^\w\*\s*]/g;
return reg_firsts_char.test(text) || reg_special_char.test(text);
}
jQuery("#profile_data_name").blur(function(){
jQuery(this).attr("class", "");
if( this.value.length > 0 ) {
if( is_invalid_formated(this.value) ) {
jQuery(this).removeClass("validated").addClass("invalid");
if(!jQuery(".full_name_error")[0]) {
var message = jQuery("#full_name_error").val();
jQuery(this).parent().append("<span class='full_name_error'>"+message+"</span>");
} else {
jQuery(".full_name_error").show();
}
} else {
jQuery(this).removeClass("invalid").addClass("validated");
jQuery(".full_name_error").hide();
}
}
});
}
jQuery(document).ready(function(){
set_initial_form_custom_data();
jQuery('#secondary_email_field').blur(
validate_email_format
);
jQuery("#user_email").blur(put_brazil_based_on_email);
jQuery('#secondary_email_field').focus(function() { jQuery('#secondary-email-balloon').fadeIn('slow'); });
jQuery('#secondary_email_field').blur(function() { jQuery('#secondary-email-balloon').fadeOut('slow'); });
jQuery("#user_pw").blur(verify_user_password_size);
jQuery("#profile_data_country").blur(show_or_hide_phone_mask);
show_or_hide_phone_mask();
fix_phone_mask_format("#profile_data_cell_phone");
fix_phone_mask_format("#profile_data_comercial_phone");
window.setTimeout(function(){
/*
Noosfero application.js is one of the last loaded javascript files.
Then, to override an application.js validation, this code waits for 2 seconds.
Or else, application.js validation override this validation
*/
set_full_name_validation();
}, 2000);
});
})();