Skip to content

Commit

Permalink
Use Validator in Validatable behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Shuwen Qian authored and anthonykoerber committed Mar 2, 2016
1 parent 17b5a06 commit 56becea
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 42 deletions.
44 changes: 7 additions & 37 deletions src/shared/behaviors/validatable.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<link rel="import" href="../js/validator.html"/>
<script>
/**
* @license
Expand All @@ -6,7 +7,9 @@
*/
(function (scope) {


var Validator = StrandLib.Validator;

scope.Validatable = {

listeners: {
Expand All @@ -24,39 +27,6 @@
}
},

// common validation rules
rules: {
email: function(i) {
var regEx = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return regEx.test(i);
},
alpha: function(i) {
var regEx = /^\w+$/;
return regEx.test(i);
},
int: function(i) {
var regEx = /^\d+$/;
return regEx.test(i);
},
decimal: function(i) {
var regEx = /^\d*[.]\d+$/;
return regEx.test(i);
},
whitespace: function(i) {
var regEx = /\s/;
return i.length > 0 && !regEx.test(i);
},
checked: function(i) {
return i === true;
},
empty: function(i) {
return i && i.length > 0;
},
blank: function(i) {
return i.trim().length > 0;
}
},

// items to validate against
testSet: null,

Expand All @@ -78,7 +48,7 @@
validate: function(value) {
if(this.validation) {
var result = this.testSet.map(function(item) {
return this.rules[item](value);
return Validator.rules[item](value);
}, this).filter(function(item) {
return item === true;
});
Expand All @@ -90,5 +60,5 @@

};

})(window.StrandTraits = window.StrandTraits || {});
</script>
})(window.StrandTraits = window.StrandTraits || {});
</script>
10 changes: 5 additions & 5 deletions src/shared/js/validator.html
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<script src="../../../bower_components/validator-js/validator.min.js"/></script>
<script type="text/javascript">
(function (scope) {

// default rules for common cases:
scope.Validator = {
rules: {
email: function(i) {
return validator.isEmail(i);
},
alpha: function(i) {
return validation.isAlpha(i);
return validator.isAlpha(i);
},
int: function(i) {
return validator.isInt(i);
return validator.isInt(i);
},
decimal: function(i) {
return validator.isDecimal(i);
Expand All @@ -32,5 +32,5 @@
}
};

})(window.StrandLib = window.StrandLib || {});
</script>
})(window.StrandLib = window.StrandLib || {});
</script>

0 comments on commit 56becea

Please sign in to comment.