Skip to content

Commit aa595bc

Browse files
committed
Update name validation on form entries
1 parent 99a3bbc commit aa595bc

1 file changed

Lines changed: 29 additions & 21 deletions

File tree

src/forms/form.js

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,18 @@ function isLikelyInvalidName(name) {
160160

161161
const vowels = name.match(/[aeiou]/gi) || [];
162162
const vowelRatio = vowels.length / name.replace(/\s/g, '').length;
163-
if (vowelRatio < 0.15 || vowelRatio > 0.6) score += 2;
163+
if (vowelRatio < 0.15 || vowelRatio > 0.7) score += 2;
164164

165165
if (/asdf|qwer|zxcv|hjkl/i.test(name)) score += 3;
166166

167167
if (/[^aeiou\s]{6,}/i.test(name)) score += 2;
168168

169-
if (/[^a-z\s'-]/i.test(name)) score += 2;
169+
if (
170+
/[^a-zàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿāăąćčďđēėęěğģīįķĺļľłńņňőœŕřśşšţťũūůűųźżžșțə\s'-]/i.test(
171+
name
172+
)
173+
)
174+
score += 3;
170175

171176
const capsScore = checkCapitalization(name);
172177
score += capsScore;
@@ -179,34 +184,37 @@ function checkCapitalization(name) {
179184

180185
let suspiciousPatterns = 0;
181186

182-
const allLowercase = !/[A-Z]/.test(name);
183-
184187
for (const word of words) {
185188
if (word.length === 0) continue;
186189

187-
const capitals = (word.match(/[A-Z]/g) || []).length;
188-
189-
if (word.length > 2 && capitals === word.length) {
190-
suspiciousPatterns += 1;
190+
if (/[A-Z]{3,}/.test(word)) {
191+
suspiciousPatterns += 2;
191192
}
192193

193194
if (word.length > 2) {
194-
const randomCaps = /^[a-z]+[A-Z][a-z]*[A-Z]|^[A-Z][a-z]+[A-Z][a-z]+[A-Z]/;
195-
const isException = /^(Ma?c|O'|D'|De|Van|Von|La|Le|Di|Da)[A-Z]/i.test(word);
196-
197-
if (randomCaps.test(word) && !isException) {
198-
suspiciousPatterns += 1;
195+
const isException = /^(ma?c|o'|d'|de|van|von|la|le|di|da)[A-Z]/i.test(
196+
word
197+
);
198+
if (isException) {
199+
return suspiciousPatterns;
199200
}
200-
}
201-
}
202201

203-
if (!allLowercase && words.length > 1) {
204-
const hasLowercaseWord = words.some((w) => w.length > 2 && /^[a-z]+$/.test(w));
205-
const hasUppercaseWord = words.some((w) => w.length > 2 && /^[A-Z]+$/.test(w));
206-
const hasProperCaseWord = words.some((w) => /^[A-Z][a-z]+$/.test(w));
202+
const startsWithLowerCase = /^[a-z]/.test(word);
207203

208-
if ((hasUppercaseWord && hasLowercaseWord) || (hasProperCaseWord && hasLowercaseWord)) {
209-
suspiciousPatterns += 1;
204+
let transitions = 0;
205+
for (let i = 1; i < word.length; i++) {
206+
const prevIsUpper = /[A-Z]/.test(word[i - 1]);
207+
const currIsUpper = /[A-Z]/.test(word[i]);
208+
if (prevIsUpper !== currIsUpper) {
209+
transitions++;
210+
}
211+
}
212+
213+
if (startsWithLowerCase && transitions > 0) {
214+
suspiciousPatterns += 3;
215+
} else if (!startsWithLowerCase && transitions > 2) {
216+
suspiciousPatterns += 3;
217+
}
210218
}
211219
}
212220

0 commit comments

Comments
 (0)