Sanitize user input: Difference between revisions

→‎{{header|Wren}}: Tightened up rules for acceptable names in line with revised task requirements.
m (→‎{{header|Phix}}: or whitelist as per Raku)
(→‎{{header|Wren}}: Tightened up rules for acceptable names in line with revised task requirements.)
Line 82:
{{libheader|Wren-trait}}
<br>
The following assumes that names are only valid if they contain ASCII letters, hyphens or apostrophes. However, the first or last character of a name can't be a punctuation character and a name must be between 1 and 20 characters long. A single character name is allowed to cater for an initial where the full name is not known. People are given a chance to abbreviate their names if they are too long.
I'll start by saying that I agree with everything that was said in the Raku entry but, in the interests of writing some code, I've taken a very simplistic view of which names are acceptable if, say, we're trying to build a database.
 
No other characters are allowed including control characters, spaces, symbols, emojis and non-English letters. Names which include them are simply rejected.
Basically, names are only valid if they contain letters or digits (yes, digits have been known to be used) in the ISO 8859 range and also hyphens, underscores or apostrophes. However, the first or last character of a name can't be a punctuation character.
 
Furthermore, that there is a blacklist of unacceptable names though in practice this would probably be much longer or more sophisticated than the one I've used here, depending on what will be done with the records later.
<lang ecmascript>import "/ioutil" for Input
import "/pattern" for Pattern
Line 108:
"drop", "delete", "erase", "kill", "wipe", "remove",
"file", "files", "directory", "directories",
"table", "tables", "record", "records", "database", "databases",
"system", "system32", "system64", "rm", "rf", "rmdir", "format", "reformat"
]
 
var punct = "'-_\xad" // allowable punctuation
var p = Pattern.new("+1&y", Pattern.whole)
var i = Pattern.letter + punct
var punct = "'-_\xad" // allowable punctuation
var p = Pattern.new("+1&yi", Pattern.whole, i)
 
var sanitizeInput = Fn.new { |name|
Line 144 ⟶ 146:
 
{{out}}
Sample (abridged) input/output:. The ninth person's name contains a tab character.
<pre>
Enter your first name : DonaldMickey_mouse
Enter your last name : Duck
 
Enter your first name : Mickey Mouse
Sorry, your name contains unacceptable characters.
 
Line 157 ⟶ 156:
 
Enter your first name : Fred
Enter your last name : rm -rf /
Sorry, your name contains unacceptable characters.
 
Line 164 ⟶ 163:
Sorry, your name is unacceptable.
 
Enter your first name : NicolasBeyoncé
Sorry, your name contains unacceptable characters.
Enter your last name : Pépé
 
Enter your first name : MarilynA-12
Sorry, your name contains unacceptable characters.
Enter your last name : Monroe
 
Enter your first name : Bridget'Andrew-
Sorry, your name contains unacceptable characters.
Enter your last name : O'Riley
 
Enter your first name : 'Prince-👨👨‍👩‍👦
Sorry, your name contains unacceptable characters.
 
Enter your first name : Don ald
Sorry, your name contains unacceptable characters.
 
Enter your first name : Mickey MouseEric
Enter your last name : Schäfer
Sorry, your name contains unacceptable characters.
 
Line 179 ⟶ 185:
Enter your last name : Wolfeschlegelsteinhausenbergerdorff
Must have a length between 1 and 20 characters, try again.
Enter your last name : Wolfeschlegelstein'hf
 
Enter your last first name : MonroeMarilyn
Enter your last name : DuckMonroe
 
Enter your last first name : O'RileyBridget
Enter your last name : PépéO'Riley
 
... (plus another 107 acceptable people)
 
The following 1510 person(s) have been added to the database:
Blaine Wolfeschlegelstein'hf
Donald Duck
Nicolas Pépé
Marilyn Monroe
Bridget O'Riley
... (10plus 7 more)
Blaine Wolfeschlegelstein'h
... (10 more)
</pre>
9,476

edits