live validation on form, change special character definition

The internal array of special characters was lacking, so a character is
now special when not a digit and (uppercase form) == (lowercase form).
pull/35/head v0.2.6
Harvey Tindall 4 years ago
parent 6860933498
commit 4aae655180
No known key found for this signature in database
GPG Key ID: BBC65952848FB1A2

@ -8,13 +8,11 @@ import (
type Validator struct { type Validator struct {
minLength, upper, lower, number, special int minLength, upper, lower, number, special int
criteria ValidatorConf criteria ValidatorConf
specialChars []rune
} }
type ValidatorConf map[string]int type ValidatorConf map[string]int
func (vd *Validator) init(criteria ValidatorConf) { func (vd *Validator) init(criteria ValidatorConf) {
vd.specialChars = []rune{'[', '@', '_', '!', '#', '$', '%', '^', '&', '*', '(', ')', '<', '>', '?', '/', '\\', '|', '}', '{', '~', ':', ']'}
vd.criteria = criteria vd.criteria = criteria
} }
@ -40,14 +38,10 @@ func (vd *Validator) validate(password string) map[string]bool {
count["lowercase"] += 1 count["lowercase"] += 1
} else if unicode.IsNumber(c) { } else if unicode.IsNumber(c) {
count["number"] += 1 count["number"] += 1
} else { } else if unicode.ToUpper(c) == unicode.ToLower(c) {
for _, s := range vd.specialChars {
if c == s {
count["special"] += 1 count["special"] += 1
} }
} }
}
}
results := map[string]bool{} results := map[string]bool{}
for criterion, num := range count { for criterion, num := range count {
if num < vd.criteria[criterion] { if num < vd.criteria[criterion] {

@ -117,7 +117,7 @@ form.onsubmit = create;
class Requirement { class Requirement {
private _name: string; private _name: string;
private _minCount: number; protected _minCount: number;
private _content: HTMLSpanElement; private _content: HTMLSpanElement;
private _valid: HTMLSpanElement; private _valid: HTMLSpanElement;
private _li: HTMLLIElement; private _li: HTMLLIElement;
@ -151,8 +151,13 @@ class Requirement {
} }
this._content.textContent = text; this._content.textContent = text;
} }
validate = (count: number) => { this.valid = (count >= this._minCount); }
} }
// Incredible code right here
const isInt = (s: string): boolean => { return (s in ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]); }
const testStrings = (f: pwValString): boolean => { const testStrings = (f: pwValString): boolean => {
const testString = (s: string): boolean => { const testString = (s: string): boolean => {
if (s == "" || !s.includes("{n}")) { return false; } if (s == "" || !s.includes("{n}")) { return false; }
@ -161,6 +166,32 @@ const testStrings = (f: pwValString): boolean => {
return testString(f.singular) && testString(f.plural); return testString(f.singular) && testString(f.plural);
} }
interface Validation { [name: string]: number }
const validate = (s: string): Validation => {
let v: Validation = {};
for (let criteria of ["length", "lowercase", "uppercase", "number", "special"]) { v[criteria] = 0; }
v["length"] = s.length;
for (let c of s) {
if (isInt(c)) { v["number"]++; }
else {
const upper = c.toUpperCase();
if (upper == c.toLowerCase()) { v["special"]++; }
else {
if (upper == c) { v["uppercase"]++; }
else if (upper != c) { v["lowercase"]++; }
}
}
}
return v
}
passwordField.addEventListener("keyup", () => {
const v = validate(passwordField.value);
for (let criteria in requirements) {
requirements[criteria].validate(v[criteria]);
}
});
var requirements: { [category: string]: Requirement} = {}; var requirements: { [category: string]: Requirement} = {};
if (!window.validationStrings) { if (!window.validationStrings) {

Loading…
Cancel
Save