From 415d759fb670f4d04fc6dc987eb149a214f8b2d3 Mon Sep 17 00:00:00 2001 From: Dennis Date: Sun, 13 Oct 2019 12:08:23 +0200 Subject: [PATCH] Added validation of number of guests If number of guests is 255 (which is the default value), no email will be sent to the recepient. Most normal users have Javascript enabled, so their browser should throw an error if the number of guests isn't filled out. --- src/main.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 49780d4..741c51e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -106,6 +106,15 @@ fn validate_email(email: &String) -> bool { Regex::new(r"^[a-zA-Z0-9\.\-_]+@[a-zA-Z0-9\-\.]+\.[a-zA-Z]+$").unwrap().is_match(email) } +fn validate_guests(guests: &u8) -> bool { + // Check if the default number of guests (255) changed. + if guests < &255 { + true + } else { + false + } +} + fn strip_tld(url: &str) -> Option { // Get the top level domain which is used to determine the language @@ -204,7 +213,7 @@ fn submit_task (host: HostHeader, Some(x) => { // Check input for anything strange or something that does not look like // an email address. - if validate_name(&x.name) && validate_email(&x.email) { + if validate_name(&x.name) && validate_email(&x.email) && validate_guests(&x.guests){ // Create HashMap to use with strfnt let mut vars = HashMap::new(); vars.insert("name".to_string(), &x.name); @@ -255,7 +264,7 @@ fn submit_task (host: HostHeader, &settings); } } else { - result = Err("Validate e-mail or name went wrong.".to_string()); + result = Err("Validate e-mail, name, or number of guests went wrong.".to_string()); } // Send a message to the sender's e-mailaddres to inform about