Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validator for MAC address. #458

Merged
merged 3 commits into from
Dec 1, 2015
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 macAddressMaybe = /^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$/;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why macAddressMaybe and not macAddress? The IP and ISBN regexes are named as such because the functions contain additional validation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I renamed the variable.


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 macAddressMaybe.test(str);
};

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