s-valid
Version:
Simple validator for strings
20 lines (15 loc) • 457 B
text/typescript
// ----- zip code
// -- 1. input must be a string
// -- 2. check number
// -- 3. perform regex test
// ---------------------------------------
export default function(s: string) {
// [1] throws here if not a string
s = s.toLowerCase();
// [2] every zip code begins with a number, and none are < 00501
const numeric = parseInt(s, 10);
if (isNaN(numeric) || numeric < 501) return false;
// [3]
const regex = /^\d{5}$/;
return regex.test(s);
};