Skip to content

Instantly share code, notes, and snippets.

@tolawho
Created September 15, 2016 10:24
Show Gist options
  • Save tolawho/55fb86c7b198de0cbbd47adf92916d98 to your computer and use it in GitHub Desktop.
Save tolawho/55fb86c7b198de0cbbd47adf92916d98 to your computer and use it in GitHub Desktop.
app.js trong một dự án nhỏ của khách hàng.
var app = {
data: {},
jsf: {
extend: function (o) {
$.extend(true, app.data, o);
}
},
msg: function(key){
return (typeof app.data.msg[key] === 'undefined')? 'Message undefined' : app.data.msg[key];
},
property: {
add: {
init: function() {
app.library.validate('registrationForm', {
file_upload_new: {
required: true
}
},
{
file_upload_new: {
required: app.msg('Property.add.required')
}
}
);
}
},
edit: {
init: function() {
app.library.validate('editForm', {
property_name: {
required: function(element){
return $("#file_upload_edit").val()=="";
},
maxlength: 255
},
property_address: {
required: function(element){
return $("#file_upload_edit").val()=="";
},
maxlength: 255
}
},
{
property_name: {
required: app.msg('Property.name.required'),
maxlength: app.msg('Property.name.max')
},
property_address: {
required: app.msg('Property.address.required'),
maxlength: app.msg('Property.address.max')
}
}
);
}
},
search: {
init: function () {
app.library.validate('searchForm', {
property_id: {
hankaku:true
}
},
{
property_id: {
hankaku:app.msg('registration.propertyNo.hankaku')
}
}
);
}
}
},
resident: {
edit: {
init: function () {
app.library.validate('residentForm',{
email1: {
emailallownull: true,
maxlength: 255
},
email2: {
emailallownull: true,
maxlength: 255
},
email3: {
emailallownull: true,
maxlength: 255
},
card_no1: {
digits: true,
rangelength: [5, 10]
},
card_no2: {
digits: true,
rangelength: [5, 10]
},
card_no3: {
digits: true,
rangelength: [5, 10]
},
credit_card: {
regex: /^$|^[a-zA-Z0-9-]+$/,
rangelength: [1, 16],
},
tel: {
digits: true,
maxlength: 11
},
name: {
maxlength: 255
},
note: {
maxlength: 255
}
},{
email1: {
maxlength: app.msg('residentinfo.email1.max'),
emailallownull: app.msg('residentinfo.email1.email')
},
email2: {
maxlength: app.msg('residentinfo.email2.max'),
emailallownull: app.msg('residentinfo.email2.email')
},
email3: {
maxlength: app.msg('residentinfo.email3.max'),
emailallownull: app.msg('residentinfo.email3.email')
},
card_no1: {
rangelength: app.msg('residentinfo.card_no1.max'),
digits: app.msg('residentinfo.card_no1.number')
},
card_no2: {
rangelength: app.msg('residentinfo.card_no2.max'),
digits: app.msg('residentinfo.card_no2.number')
},
card_no3: {
rangelength: app.msg('residentinfo.card_no3.max'),
digits: app.msg('residentinfo.card_no3.number')
},
credit_card: {
rangelength: app.msg('residentinfo.credit_card.max'),
regex: app.msg('residentinfo.credit_card.format')
},
tel: {
maxlength: app.msg('residentinfo.tel.max'),
digits: app.msg('residentinfo.tel.number')
},
name: {
maxlength: app.msg('residentinfo.name.max')
},
note: {
maxlength: app.msg('residentinfo.note.max')
}
});
}
},
registration: {
init: function () {
app.library.validate('searchForm',{
no: {
required: true,
hankaku:true
}
},{
no: {
required: app.msg('registration.propertyNo.notEmpty'),
hankaku: app.msg('registration.propertyNo.hankaku')
}
}, 'errorSearch');
app.library.validate('createForm',{
property_no: {
required: true,
maxlength: 9,
hankaku:true
},
csv: {
required: true
}
},{
property_no: {
required: app.msg('registration.propertyNo.notEmpty'),
maxlength: app.msg('registration.propertyNo.maxLength'),
hankaku: app.msg('registration.propertyNo.hankaku')
},
csv: {
required: app.msg('registration.csv.notEmpty')
}
});
}
}
},
user: {
login:{
init: function () {
app.library.validate('loginForm',{
login_id: {
required: true
},
password: {
required: true
}
},{
login_id: {
required: app.msg('login.id.notEmpty')
},
password: {
required: app.msg('login.pwd.notEmpty')
}
});
}
},
edit:{
init: function(){
app.library.validate('editForm',{
username: {
required: true,
maxlength: 255
},
email: {
required: true,
email: true,
maxlength: 255
}
},{
username:{
required: app.msg('users.username.notEmpty'),
maxlength: app.msg('users.username.max')
},
email:{
required: app.msg('users.email.notEmpty'),
email: app.msg('users.email.format'),
maxlength: app.msg('users.email.max')
}
});
}
},
add:{
init: function(){
app.library.validate('addForm',{
login_id: {
required: true,
maxlength: 255
},
username: {
required: true,
maxlength: 255
},
email: {
required: true,
emailallownull: true,
maxlength: 255
},
password: {
required: true,
minlength: 8,
regex: /^$|^[a-zA-Z0-9]+$/,
maxlength: 10
},
confirm_password: {
required: true,
equalTo: "#password"
}
},{
login_id: {
required: app.msg('users.login_id.notEmpty'),
maxlength: app.msg('users.login_id.max')
},
username:{
required: app.msg('users.username.notEmpty'),
maxlength: app.msg('users.username.max')
},
email:{
required: app.msg('users.email.notEmpty'),
emailallownull: app.msg('users.email.format'),
maxlength: app.msg('users.email.max')
},
password:{
required: app.msg('users.password.required'),
minlength: app.msg('users.password.range'),
maxlength: app.msg('users.password.range'),
regex: app.msg('users.password.format')
},
confirm_password: {
required: app.msg('users.confirm_password.required'),
equalTo: app.msg('users.password.notmatch')
}
});
}
},
pwd:{
init: function () {
app.library.validate('changePwd', {
oldPASS: {
required: true
},
newPASS: {
required: true,
minlength: 8,
maxlength: 10,
regex: /^[a-zA-Z0-9]{8,10}$/
}
}, {
oldPASS: {
required: app.msg('changepwd.oldPASS.notEmpty')
},
newPASS: {
required: app.msg('changepwd.newPASS.notEmpty'),
minlength: app.msg('changepwd.newPASS.min'),
maxlength: app.msg('changepwd.newPASS.max'),
regex: app.msg('changepwd.newPASS.regex')
}
});
}
}
},
ajax: {
result: {},
html: function (url, data, container, call_back) {
var error = false;
$.ajax({
type: "POST",
url: url,
data: data,
dataType: "html",
async: true,
success: function (html) {
if (app.library.is_json(html)) {
app.ajax.result = app.library.parse_json(html);
if (app.ajax.result.error) {
error = true;
alert(app.ajax.result.error.msg);
}
} else {
$(container).html(html);
if (typeof call_back == 'function') {
call_back.call();
}
}
},
error: function (error) {}
});
return app.ajax.result;
},
json: function (url, data, call_back) {
$.ajax({
type: "POST",
url: url,
data: data,
dataType: "json",
async: true,
success: function (json) {
try {
app.ajax.result = json;
if (app.ajax.result.error) {
alert(app.ajax.result.error.msg);
} else {
if (typeof call_back == 'function') {
call_back.call();
}
}
} catch (e) {
alert(json);
}
},
error: function (error) {}
});
return app.ajax.result;
},
xml: function () {
$.ajax({
type: "POST",
url: url,
data: data,
dataType: "xml",
async: false,
success: function (xml) {},
error: function (error) {}
});
}
},
library: {
parse_json:function(json){return $.parseJSON(json);},
is_json:function(b){try{$.parseJSON(b)}catch(a){return false}return true},
array_intersect:function(d,c){return $.grep(d,function(a){return $.inArray(a,c)>-1})},
is_empty:function(a){if(!$.trim(a)){return true}else{return false}},
validate:function(form,rules,messages,place='error') {
$("#"+form).validate({
rules: rules,
messages: messages,
onfocusin: false,
onfocusout: false,
onkeyup: false,
onclick: false,
errorPlacement: function (error, element) {
$('#'+place).text(error.text());
},
showErrors: function (errorMap, errorList) {
var map = {};
var error = new Array(1);
if (this.numberOfInvalids() > 1) {
error[0] = errorList[0];
errorList = error;
$.each(errorMap, function (i, v) {
map[i] = v;
errorMap = map;
return false;
});
}
this.errorList = errorList;
this.errorMap = errorMap;
this.defaultShowErrors();
},
errorElement: 'div',
submitHandler: function (a) {
if ($(a).valid()) {
a.submit();
} else {
return false;
}
}
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment