Skip to content

Commit

Permalink
Refactored so numeric/integer follows the same pattern as length when…
Browse files Browse the repository at this point in the history
… specifying detailed ruling.

Added some comparison betwwen input and "null" to fix problem in IE and Form.Element#getValue() in Prototype. Will try to fix a patch for Prototype.
  • Loading branch information
wailqill committed Sep 20, 2008
1 parent 615710e commit 02b9952
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 53 deletions.
2 changes: 1 addition & 1 deletion demo/simple.html
Expand Up @@ -16,7 +16,7 @@
var fv = new wq.Form.Validation("demo-form", {
"#name": { length: { min: 5 } },
"#email": { required: true, email: true },
"#age": { required: true, integer: "positive" }
"#age": { required: true, numeric: { min: 18, max: 120 } }
});

});
Expand Down
41 changes: 21 additions & 20 deletions src/wq.form.validation.js
Expand Up @@ -115,32 +115,33 @@
return !input || option.test(input);
},
regexp: function(input, option) {
return !input || option.test(input);
return !input || input == "null" || option.test(input);
},
numeric: function(input, option) {
if (!input) return true;
if (!input || input == "null") return true;

var parse = parseInt;
var compare = function(input) {
return parse(input).toString() == input;
}

if (option === true)
return compare(input); // Any valid number

option = $H(option);
parse = option.get("real") === true ? parseFloat : parseInt;

return option.all(function(rule) {
if (rule.key == "min" && parse(input) >= rule.value && compare(input)) return true;
if (rule.key == "max" && parse(input) <= rule.value && compare(input)) return true;
if (rule.key == "real" && compare(input)) return true; // Fallback...
return false;
});

if (option == "positive") return this.numeric_positive(input);
if (option == "negative") return this.numeric_negative(input);
return input == parseFloat(input).toString();
},
numeric_positive: function(input) {
return input == Math.abs(parseFloat(input)).toString();
},
numeric_negative: function(input) {
return input == -Math.abs(parseFloat(input)).toString();
},
integer: function(input, option) {
if (!input) return true;
if (option == "positive") return this.integer_positive(input);
if (option == "negative") return this.integer_negative(input);
return input == parseInt(input).toString();
},
integer_positive: function(input) {
return input == Math.abs(parseInt(input)).toString();
},
integer_negative: function(input) {
return input == -Math.abs(parseInt(input)).toString();
},
length: function(input, option) {
if (option instanceof ObjectRange)
return option.include(input.length);
Expand Down
57 changes: 25 additions & 32 deletions test/validator_tests.html
Expand Up @@ -97,80 +97,73 @@ <h2>Validator tests</h2>
inputTextAssertFalse(this, elm, "Hello");
inputTextAssertFalse(this, elm, "-");
inputTextAssertFalse(this, elm, "+");
inputTextAssertFalse(this, elm, "1.555");

inputTextAssertTrue(this, elm, null);
inputTextAssertTrue(this, elm, "");
inputTextAssertTrue(this, elm, "1");
inputTextAssertTrue(this, elm, "111");
inputTextAssertTrue(this, elm, "-1");
inputTextAssertTrue(this, elm, "1.555");
inputTextAssertTrue(this, elm, "-0.000001");
},
test_validate_numeric_positive: function() {
var elm = new wq.Form.Validation.Field("person_field", { numeric: "positive" });
test_validate_numeric_with_minimum: function() {
var elm = new wq.Form.Validation.Field("person_field", { numeric: { min: 0 } });

inputTextAssertFalse(this, elm, "Hello");
inputTextAssertFalse(this, elm, "-");
inputTextAssertFalse(this, elm, "+");
inputTextAssertFalse(this, elm, "-123");
inputTextAssertFalse(this, elm, "-0.001");
inputTextAssertFalse(this, elm, "0.002");

inputTextAssertTrue(this, elm, null);
inputTextAssertTrue(this, elm, "");
inputTextAssertTrue(this, elm, "0.002");
inputTextAssertTrue(this, elm, "2");
inputTextAssertTrue(this, elm, "1658"); // Skåne
},
test_validate_numeric_negative: function() {
var elm = new wq.Form.Validation.Field("person_field", { numeric: "negative" });
test_validate_numeric_with_maximum: function() {
var elm = new wq.Form.Validation.Field("person_field", { numeric: { max: 0 } });

inputTextAssertFalse(this, elm, "Hello");
inputTextAssertFalse(this, elm, "-");
inputTextAssertFalse(this, elm, "+");
inputTextAssertFalse(this, elm, "123");
inputTextAssertFalse(this, elm, "0.001");
inputTextAssertFalse(this, elm, "-0.001");

inputTextAssertTrue(this, elm, null);
inputTextAssertTrue(this, elm, "");
inputTextAssertTrue(this, elm, "-0.002");
inputTextAssertTrue(this, elm, "-273.16"); // Celsius
inputTextAssertTrue(this, elm, "-1");
},
test_validate_integer: function() {
var elm = new wq.Form.Validation.Field("person_field", { integer: true });
test_validate_numeric_with_real: function() {
var elm = new wq.Form.Validation.Field("person_field", { numeric: { real: true } });

inputTextAssertFalse(this, elm, "Hello");
inputTextAssertFalse(this, elm, "-");
inputTextAssertFalse(this, elm, "+");
inputTextAssertFalse(this, elm, "1.555");
inputTextAssertFalse(this, elm, "-0.000001");

inputTextAssertTrue(this, elm, "1");
inputTextAssertTrue(this, elm, "-1");
inputTextAssertTrue(this, elm, "1.555");
inputTextAssertTrue(this, elm, "-0.000001");
},
test_validate_integer_positive: function() {
var elm = new wq.Form.Validation.Field("person_field", { integer: "positive" });
test_validate_numeric_with_minimum_real: function() {
var elm = new wq.Form.Validation.Field("person_field", { numeric: { real: true, min: -5 } });

inputTextAssertFalse(this, elm, "Hello");
inputTextAssertFalse(this, elm, "-");
inputTextAssertFalse(this, elm, "+");
inputTextAssertFalse(this, elm, "-123");
inputTextAssertFalse(this, elm, "-0.001");
inputTextAssertFalse(this, elm, "-5.1");

inputTextAssertTrue(this, elm, "12");
inputTextAssertTrue(this, elm, "1523"); // Gustav Vasa
inputTextAssertTrue(this, elm, "-4");
inputTextAssertTrue(this, elm, "4.66");
},
test_validate_integer_negative: function() {
var elm = new wq.Form.Validation.Field("person_field", { integer: "negative" });
test_validate_numeric_with_maximum_real: function() {
var elm = new wq.Form.Validation.Field("person_field", { numeric: { real: true, max: 5 } });

inputTextAssertFalse(this, elm, "Hello");
inputTextAssertFalse(this, elm, "-");
inputTextAssertFalse(this, elm, "+");
inputTextAssertFalse(this, elm, "123");
inputTextAssertFalse(this, elm, "-0.1");
inputTextAssertFalse(this, elm, "5.1");

inputTextAssertTrue(this, elm, "-2");
inputTextAssertTrue(this, elm, "-40"); // Celsius/Fahrenheit
inputTextAssertTrue(this, elm, "4");
inputTextAssertTrue(this, elm, "-273.16"); // Celsius
},
test_combinating_validations: function() {
var elm = new wq.Form.Validation.Field("person_field", { regexp: /^hello/, min_length: 10, email: true });
var elm = new wq.Form.Validation.Field("person_field", { regexp: /^hello/, length: { min: 10 }, email: true });

inputTextAssertFalse(this, elm, "hell@kitty.com");
inputTextAssertFalse(this, elm, "hello@a.c");
Expand Down

0 comments on commit 02b9952

Please sign in to comment.