Skip to content

Commit

Permalink
Merge pull request #458 from cyrilschumacher/master
Browse files Browse the repository at this point in the history
Add validator for MAC address.
  • Loading branch information
chriso committed Dec 1, 2015
2 parents 66ac635 + d00c382 commit 07d7b61
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ $ bower install validator-js
- **isJSON(str)** - check if the string is valid JSON (note: uses JSON.parse).
- **isLength(str, min [, max])** - check if the string's length falls in a range. Note: this function takes into account surrogate pairs.
- **isLowercase(str)** - check if the string is lowercase.
- **isMACAddress(str)** - check if the string is a MAC address.
- **isMobilePhone(str, locale)** - check if the string is a mobile phone number, (locale is one of `['zh-CN', 'zh-TW', 'en-ZA', 'en-AU', 'en-HK', 'pt-PT', 'fr-FR', 'el-GR', 'en-GB', 'en-US', 'en-ZM', 'ru-RU', 'nb-NO', 'nn-NO', 'vi-VN', 'en-NZ']`).
- **isMongoId(str)** - check if the string is a valid hex-encoded representation of a [MongoDB ObjectId][mongoid].
- **isMultibyte(str)** - check if the string contains one or more multibyte chars.
Expand Down
19 changes: 19 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,25 @@ describe('Validators', function () {
});
});

it('should validate MAC addresses', function () {
test({
validator: 'isMACAddress',
valid: [
'ab:ab:ab:ab:ab:ab',
'FF:FF:FF:FF:FF:FF',
'01:02:03:04:05:ab',
'01:AB:03:04:05:06'
],
invalid: [
'abc',
'01:02:03:04:05',
'01:02:03:04::ab',
'1:2:3:4:5:6',
'AB:CD:EF:GH:01:02'
]
});
});

it('should validate IP addresses', function () {
test({
validator: 'isIP'
Expand Down
6 changes: 6 additions & 0 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
var isbn10Maybe = /^(?:[0-9]{9}X|[0-9]{10})$/
, isbn13Maybe = /^(?:[0-9]{13})$/;

var macAddress = /^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$/;

var ipv4Maybe = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/
, ipv6Block = /^[0-9A-F]{1,4}$/i;

Expand Down Expand Up @@ -295,6 +297,10 @@
return true;
};

validator.isMACAddress = function (str) {
return macAddress.test(str);
};

validator.isIP = function (str, version) {
version = validator.toString(version);
if (!version) {
Expand Down
Loading

0 comments on commit 07d7b61

Please sign in to comment.