-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathtext-validation.ts
More file actions
41 lines (32 loc) · 1.1 KB
/
text-validation.ts
File metadata and controls
41 lines (32 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { text, note, isCancel } from '@clack/prompts';
import { setTimeout } from 'node:timers/promises';
async function main() {
console.clear();
// Example demonstrating the issue with initial value validation
const name = await text({
message: 'Enter your name (letters and spaces only)',
initialValue: 'John123', // Invalid initial value with numbers
validate: (value) => {
if (!/^[a-zA-Z\s]+$/.test(value)) return 'Name can only contain letters and spaces';
return undefined;
},
});
if (!isCancel(name)) {
note(`Valid name: ${name}`, 'Success');
}
await setTimeout(1000);
// Example with a valid initial value for comparison
const validName = await text({
message: 'Enter another name (letters and spaces only)',
initialValue: 'John Doe', // Valid initial value
validate: (value) => {
if (!/^[a-zA-Z\s]+$/.test(value)) return 'Name can only contain letters and spaces';
return undefined;
},
});
if (!isCancel(validName)) {
note(`Valid name: ${validName}`, 'Success');
}
await setTimeout(1000);
}
main().catch(console.error);