Created
April 15, 2013 15:09
-
-
Save aykutyaman/5388797 to your computer and use it in GitHub Desktop.
backbone validate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Backbone.Validate = function(model, changedAttributes) { | |
return (function(patterns) { | |
var isNumeric = function(value){ | |
return _.isNumber(value) || | |
(_.isString(value) && value.match(patterns.number)); | |
}; | |
var hasValue = function(value) { | |
return !(_.isNull(value) || _.isUndefined(value) || (_.isString(value) && value === '')); | |
}; | |
this.errors = {}; | |
this.attributes = _.clone(model.attributes); | |
_.extend(this.attributes, changedAttributes); | |
this.addError = function(field, message) { | |
if (_.isUndefined(this.errors[field])) { | |
this.errors[field] = []; | |
} | |
this.errors[field].push(message); | |
}; | |
var self = this; | |
var getModelAttrValue = function(field) { | |
if (field.indexOf('.') === -1) { | |
return self.attributes[field]; | |
} else {//deep model attribute | |
return eval('self.attributes.' + field); | |
} | |
}; | |
this.myValidators = { | |
length: function(field, value) { | |
if (hasValue(getModelAttrValue(field)) && (getModelAttrValue(field).length !== value)) { | |
self.addError(field, value + " karakter uzunluğunda olmalı"); | |
} | |
}, | |
numeric: function(field, value) { | |
if (!isNumeric(self.attributes[field])) { | |
self.addError(field, 'Yalnız rakam giriniz'); | |
} | |
}, | |
alphaNumeric: function(field, value) { | |
var regex = new RegExp(patterns.alphaNumeric); | |
if ((!_.isEmpty(getModelAttrValue(field)) && (!getModelAttrValue(field).match(regex)))) { | |
self.addError(field, 'sadece rakam ve harflerden oluşabilir '); | |
} | |
}, | |
rangeLength: function(field, value) { | |
if(!hasValue(value) || getModelAttrValue(field).length < value[0] || | |
getModelAttrValue(field).length > value[1]) { | |
self.addError(field, 'Yazı ' + value[0] + ' - ' + value[1] + ' karakter arası olmalı'); | |
} | |
}, | |
required: function(field, value) { | |
if (_.isEmpty(getModelAttrValue(field))) { | |
self.addError(field, 'Bu alanı doldurulmak zorunludur'); | |
} | |
}, | |
range: function(field, value) { | |
if (!isNumeric(getModelAttrValue(field)) || | |
getModelAttrValue(field) < value[0] || | |
getModelAttrValue(field) > value[1]) { | |
self.addError(field, 'Alan istenilen aralikta degil'); | |
} | |
}, | |
min: function(field, value) { | |
if (!isNumeric(getModelAttrValue(field)) || getModelAttrValue(field) < value) { | |
self.addError(field, 'Minimum değer ' + value + ' olmalı'); | |
} | |
}, | |
specialChar: function(field, value) { | |
var specialChar = new RegExp(patterns.specialChar); | |
if (!_.isString(getModelAttrValue(field)) || getModelAttrValue(field).match(specialChar)) { | |
self.addError(field, 'Uygun Olmayan Karakter'); | |
} | |
}, | |
phone: function(field, value) { | |
var phoneRegex = new RegExp(patterns.phonePatern); | |
if (!getModelAttrValue(field).match(phoneRegex)) { | |
self.addError(field, 'Telefon Numarası Formatına uygun Olmayan bilgi '); | |
} | |
}, | |
turkishGSM: function(field, value) { | |
var phoneRegex = new RegExp(patterns.turkishGSM); | |
if ((!_.isEmpty(getModelAttrValue(field)) && (!getModelAttrValue(field).match(phoneRegex)))) { | |
self.addError(field, 'Hatalı cep telefonu numarası. Format: 0-5xx-xxx-xxxx'); | |
} | |
}, | |
email: function(field, value) { | |
var emailRegex = new RegExp(patterns.email); | |
if (!_.isString(getModelAttrValue(field)) || !getModelAttrValue(field).match(emailRegex)) { | |
self.addError(field, 'Hatalı e-posta formatı'); | |
} | |
} | |
}; | |
for (var validateAttr in model.validations) { | |
for (var validateRule in model.validations[validateAttr]) { | |
if (validateRule != 'msg') { | |
this.myValidators[validateRule](validateAttr, model.validations[validateAttr][validateRule]); | |
} | |
} | |
} | |
return this.errors; | |
})(Backbone.Validate.patterns, Backbone.Validate.messages); | |
}; | |
Backbone.Validate.patterns = { | |
phonePatern:/^\0([\d]{0,3})\.([\d]{7,10})$/, | |
turkishGSM: /^0[-. ]?5([\d]{2})[-. ]?([\d]{3})[-. ]?([\d]{2})[-. ]?([\d]{2})$/, | |
alphaNumeric: /^[A-Za-z0-9]*$/, | |
specialChar:/[\*\'\"\<\>]/, | |
number: /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/, | |
email: /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment