Last active
August 29, 2015 14:13
-
-
Save dz1984/8818f30fbbe557507bac to your computer and use it in GitHub Desktop.
Utility functions in Javascript.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Utility JS</title> | |
<link href="http://code.jquery.com/qunit/qunit-git.css" rel="stylesheet" type="text/css"> | |
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> | |
<script src="http://code.jquery.com/qunit/qunit-git.js"></script> | |
</head> | |
<body> | |
<div id="qunit"></div> | |
<div id="qunit-fixture"></div> | |
<script src="Utility.js"></script> | |
<script src="test.js"></script> | |
<script> | |
QUnit.test("Version check", function(assert){ | |
assert.ok(typeof Utility.version() === 'string'); | |
}); | |
QUnit.test("Empty String", function(assert){ | |
assert.ok(Utility.isEmptyString("")); | |
}); | |
QUnit.test("Taiwan personal id", function( assert ) { | |
assert.ok(Utility.isValidTaiwanPersonalId('A123456789')); | |
assert.ok(Utility.isInvalidTaiwanPersonalId('A34569')); | |
}); | |
QUnit.test("Cell phone", function(assert) { | |
assert.ok(Utility.isValidCellPhone('0910111111')); | |
assert.ok(Utility.isInvalidCellPhone('022021234')); | |
}); | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(root, factory) { | |
"use strict"; | |
if (typeof define === "function" && define.amd) { | |
define(["jquery"], factory); | |
} else if (typeof exports === "object") { | |
module.exports = factory(require("jquery")); | |
} else { | |
root.Utility = factory(root.jQuery); | |
} | |
})(this, function($) { | |
"use strict"; | |
var exports = { | |
'MIN_LEN' : 0, | |
'MAX_LEN' : 999, | |
}; | |
exports.isEmpty = function(content) { | |
var content = $.trim(content); | |
return (content === ""); | |
}; | |
exports.isNotEmpty = function(content) { | |
return false === exports.isEmpty(content); | |
}; | |
exports.isValidEmail = function(email) { | |
var email = $.trim(email); | |
var validEmailPattern = /^[A-Z0-9._%+-]+@([A-Z0-9-]+\.)+[A-Z]{2,4}$/i; | |
return validEmailPattern.test(email); | |
}; | |
exports.isInvalidEmail = function(email) { | |
return false === exports.isValidEmail(email); | |
}; | |
exports.isValidCellPhone = function(phone) { | |
var phone = $.trim(phone); | |
var validCellPhonePattern = /^[^a-zA-Z]+$/i; | |
return validCellPhonePattern.test(phone); | |
}; | |
exports.isInvalidCellPhone = function(phone) { | |
return false === exports.isValidCellPhone(phone); | |
}; | |
exports.isValidLen = function(content, maxLen, minLen) { | |
// TODO : check type of the maxLen and the minLen arguments. | |
if (typeof minLen === 'undefined') { | |
minLen = exports.MIN_LEN; | |
} | |
if (typeof maxLen === 'undefined') { | |
maxLen = exports.MAX_LEN; | |
} | |
var content = $.trim(content); | |
return (content.length > minLen && content.length <= maxLen); | |
}; | |
exports.isInvalidLen = function(content, maxLen, minLen) { | |
return false === exports.isValidLen(content, maxLen, minLen); | |
}; | |
exports.isValidAlphaNumeric = function(alphanumeric) { | |
var alphanumeric = $.trim(alphanumeric); | |
var validAlphaNumericPattern = /^\w+$/; | |
return validAlphaNumericPattern.test(alphanumeric); | |
}; | |
exports.isInvalidAlphaNumeric = function(alphanumeric) { | |
return false === exports.isValidAlphaNumeric(alphanumeric); | |
}; | |
exports.isValidNumeric = function(numeric) { | |
var numeric = $.trim(numeric); | |
var validNumericPattern = /^-?\d+(.\d+)?$/; | |
return validNumericPattern.test(numeric); | |
}; | |
exports.isInvalidNumeric = function(numeric) { | |
return false === exports.isValidNumeric(numeric); | |
}; | |
exports.isNagative = function(numeric) { | |
var numeric = parseInt($.trim(numeric)) || 0; | |
return (numeric < 0) ? true : false; | |
}; | |
exports.isNotNagative = function(numeric) { | |
return false === exports.isNagative(numeric); | |
}; | |
/** | |
* the library version. | |
*/ | |
exports.version = function() { | |
return 'v0.0.1'; | |
}; | |
/** | |
* initial this lib. | |
*/ | |
exports.init = function(_$) { | |
return init(_$ || $); | |
}; | |
return exports; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment