diff --git a/README.md b/README.md index 898c063..ce5699d 100755 --- a/README.md +++ b/README.md @@ -196,6 +196,10 @@ The field under validation may have alpha-numeric characters, as well as dashes The field under validation must be entirely alpha-numeric characters. +#### alpha_space + +The field under validation may have alphabetic characters, as well as space. + #### array The field under validation must be an array. diff --git a/spec/alpha_space-rule.js b/spec/alpha_space-rule.js new file mode 100644 index 0000000..85a29c5 --- /dev/null +++ b/spec/alpha_space-rule.js @@ -0,0 +1,32 @@ +const { Validator, expect } = require("./setup.js"); + +describe("alpha_space validation rule", function() { + it("should fail with non alphabetic-space characters", function() { + const validator = new Validator({ name: "Daniel_." }, { name: "alpha_space" }); + expect(validator.fails()).to.be.true; + expect(validator.passes()).to.be.false; + }); + + it("should fail with non-alphabetic characters", function() { + const validator = new Validator({ name: 12 }, { name: "alpha_space" }); + expect(validator.fails()).to.be.true; + expect(validator.passes()).to.be.false; + }); + + it("should pass with only alphabetic-space characters", function() { + const validator = new Validator({ name: "Daniel Naranjo" }, { name: "alpha_space" }); + expect(validator.fails()).to.be.false; + expect(validator.passes()).to.be.true; + }); + + it("should pass when the field is an empty string", function() { + const validator = new Validator({ name: "" }, { name: "alpha_space" }); + expect(validator.passes()).to.be.true; + }); + + it("should pass when the field does not exist", function() { + const validator = new Validator({}, { name: "alpha_space" }); + expect(validator.passes()).to.be.true; + expect(validator.fails()).to.be.false; + }); +}); diff --git a/spec/error-messages.js b/spec/error-messages.js index ca3389e..17c4f34 100755 --- a/spec/error-messages.js +++ b/spec/error-messages.js @@ -77,6 +77,14 @@ describe("Error messages", function() { ); }); + it("should fail with non alpha space characters", function() { + const validator = new Validator({ name: "Daniel ." }, { name: "alpha_space" }); + expect(validator.passes()).to.be.false; + expect(validator.errors.first("name")).to.equal( + "The name field may only contain alphabetic characters, as well as space." + ); + }); + it("should fail without a matching confirmation field for the field under validation", function() { const validator = new Validator({ password: "abc" }, { password: "confirmed" }); expect(validator.passes()).to.be.false; diff --git a/src/rules.js b/src/rules.js index a865d50..1973c1f 100755 --- a/src/rules.js +++ b/src/rules.js @@ -272,6 +272,10 @@ var rules = { return /^[a-zA-Z0-9]+$/.test(val); }, + alpha_space: function (val) { + return /^[a-zA-Z ]+$/.test(val); + }, + same: function (val, req) { var val1 = this.validator._flattenObject(this.validator.input)[req]; var val2 = val;