Skip to content

Commit 07d7b61

Browse files
committed
Merge pull request #458 from cyrilschumacher/master
Add validator for MAC address.
2 parents 66ac635 + d00c382 commit 07d7b61

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ $ bower install validator-js
6464
- **isJSON(str)** - check if the string is valid JSON (note: uses JSON.parse).
6565
- **isLength(str, min [, max])** - check if the string's length falls in a range. Note: this function takes into account surrogate pairs.
6666
- **isLowercase(str)** - check if the string is lowercase.
67+
- **isMACAddress(str)** - check if the string is a MAC address.
6768
- **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']`).
6869
- **isMongoId(str)** - check if the string is a valid hex-encoded representation of a [MongoDB ObjectId][mongoid].
6970
- **isMultibyte(str)** - check if the string contains one or more multibyte chars.

test/validators.js

+19
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,25 @@ describe('Validators', function () {
397397
});
398398
});
399399

400+
it('should validate MAC addresses', function () {
401+
test({
402+
validator: 'isMACAddress',
403+
valid: [
404+
'ab:ab:ab:ab:ab:ab',
405+
'FF:FF:FF:FF:FF:FF',
406+
'01:02:03:04:05:ab',
407+
'01:AB:03:04:05:06'
408+
],
409+
invalid: [
410+
'abc',
411+
'01:02:03:04:05',
412+
'01:02:03:04::ab',
413+
'1:2:3:4:5:6',
414+
'AB:CD:EF:GH:01:02'
415+
]
416+
});
417+
});
418+
400419
it('should validate IP addresses', function () {
401420
test({
402421
validator: 'isIP'

validator.js

+6
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
var isbn10Maybe = /^(?:[0-9]{9}X|[0-9]{10})$/
5353
, isbn13Maybe = /^(?:[0-9]{13})$/;
5454

55+
var macAddress = /^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$/;
56+
5557
var ipv4Maybe = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/
5658
, ipv6Block = /^[0-9A-F]{1,4}$/i;
5759

@@ -295,6 +297,10 @@
295297
return true;
296298
};
297299

300+
validator.isMACAddress = function (str) {
301+
return macAddress.test(str);
302+
};
303+
298304
validator.isIP = function (str, version) {
299305
version = validator.toString(version);
300306
if (!version) {

0 commit comments

Comments
 (0)