Elysia Tools
Mobile Navigation
Validierung
Begrenzte Prüfung ohne Regex
Kurze ASCII-Labels prüfen, bis zu drei Ziffern lesen und begrenzte Wörter ohne Regex finden.
Beispiele
Einträge in dieser Sammlung
Kurzes ASCII-Label prüfen
Länge vor höchstens 12 Zeichen prüfen; nur kleine ASCII-Buchstaben und Ziffern, ohne Regex.
function validLabel(value) {
if (typeof value !== 'string' || value.length < 1 || value.length > 12) return false;
for (const c of value) {
if (!((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9'))) return false;
}
return true;
}
const result = ['blue7', 'blue sky', ''].map(validLabel);
console.log(JSON.stringify(result));Höchstens drei Ziffern lesen
Ein bis drei ASCII-Ziffern arithmetisch sammeln; ein Buchstabe ergibt null.
function smallDecimal(value) {
if (typeof value !== 'string' || value.length < 1 || value.length > 3) return null;
let total = 0;
for (const c of value) {
if (c < '0' || c > '9') return null;
total = total * 10 + c.charCodeAt(0) - 48;
}
return total;
}
const result = ['42', '007', '7a'].map(smallDecimal);
console.log(JSON.stringify(result));Begrenztes Wort wörtlich suchen
indexOf mit höchstens 64 Text- und 12 Suchzeichen; nullbasierte Position, kein Muster.
function findWord(text, word) {
if (typeof text !== 'string' || typeof word !== 'string') return -1;
if (text.length > 64 || word.length < 1 || word.length > 12) return -1;
return text.indexOf(word);
}
const result = findWord('blue sky', 'sky');
console.log(JSON.stringify(result));Werkzeuge
Tools, die oft mit diesem Beispiel genutzt werden
Verwandt
Verwandte Beispielsammlungen
Verwandt