Skip to content

Instantly share code, notes, and snippets.

@taiyoh
Created July 17, 2009 09:09
Show Gist options
  • Save taiyoh/148957 to your computer and use it in GitHub Desktop.
Save taiyoh/148957 to your computer and use it in GitHub Desktop.
// very very simple js validation?
var Validation = {
form : {
nickname : [[
function (data) { return data.length <= 10; },
'10文字以内で入力してください'
]],
rating : [[
function (data) { return 1 <= data && data <= 5; },
'1〜5までの数字からお選びください'
]],
comment : [[
function (data) { return data.length <= 150; },
'150文字以内で入力してください'
]]
},
check : function(form) {
var self = this;
var params = {};
$(form.serialize().split('&')).each(function() {
var kv = this.split('=');
params[kv[0]] = decodeURIComponent(kv[1]);
});
var isValid = true;
$.each(self.form, function(k, funcs) {
$.each(funcs, function(i, fn) {
if (!fn[0](params[k])) {
if (!self.error[k]) self.error[k] = [];
self.error[k].push(fn[1]);
isValid = false;
}
return isValid;
});
return isValid;
});
return isValid;
},
error : {}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment