-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.js
More file actions
31 lines (25 loc) · 1.07 KB
/
validation.js
File metadata and controls
31 lines (25 loc) · 1.07 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
/**
* Validate form input, appending error message whenever errors are encountered
* @param form the form object to check
*/
function validate(form) {
// Check file name input, appending error message everytime a check fails
error = validateName(form.malware_name.value)
// Validation is successful if error messages were not appended (string is empty)
if (error == "") { return true }
// If validation of name fails, display generated error messages
else { alert(error); return false }
}
/**
* Validate name string, appending error message whenever errors are encountered
* @param name the name to check
*/
function validateName(name) {
name = name.trim()
// Check #1 - Name cannot be an empty string
if (name == "") return "Name for malware cannot be empty.\n"
// Check #2 - Name contains only english letters (case insensitive), digits, _, -
else if (/[^a-zA-Z0-9_-]/.test(name)) return "Only letters, digits, _, and - are allowed in a username.\n"
// Checks passed - Return empty string indicating no error
return "";
}