This is an automated email from the ASF dual-hosted git repository. bsalzano pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/openserverless-tutorial.git
commit 5596f00531b58f5c83916cf4cb6eaa96c9491d53 Author: Bruno Salzano <[email protected]> AuthorDate: Tue Mar 18 00:10:30 2025 +0100 form-validation --- packages/contact/submit.js | 69 ++++++++++++++++++++++++++++++++++++++++++++++ web/index.html | 3 +- 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/packages/contact/submit.js b/packages/contact/submit.js new file mode 100644 index 0000000..f531678 --- /dev/null +++ b/packages/contact/submit.js @@ -0,0 +1,69 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//--web true +//--kind nodejs:default + +function main(args) { + let message = []; + let errors = []; + + // validate the name + if (args.name) { + message.push("name: " + args.name) + } else { + errors.push("No name provided") + } + + // validate the email + var re = /\S+@\S+\.\S+/; + if (args.email && re.test(args.email)) { + message.push("email: " + args.email); + } else { + errors.push("Email missing or incorrect."); + } + + // validate the phone + if (args.phone && args.phone.match(/\d/g).length >= 10) { + message.push("phone: " + args.phone); + } else { + errors.push("Phone number missing or incorrect."); + } + + // validate the message + if (args.message) { + message.push("message:" + args.message); + } + + // return the result + if (errors.length) { + var errs = "<ul><li>" + errors.join("</li><li>") + "</li></ul>"; + return { + body: "<h1>Errors!</h1>" + + errs + '<br><a href="javascript:window.history.back()">Back</a>' + }; + } else { + var data = "<pre>" + message.join("\n") + "</pre>"; + return { + body: "<h1>Thank you!</h1>" + data, + name: args.name, + email: args.email, + phone: args.phone, + message: args.message + }; + } +} diff --git a/web/index.html b/web/index.html index 01917f5..b00a1d0 100644 --- a/web/index.html +++ b/web/index.html @@ -32,7 +32,8 @@ <div class="container d-flex justify-content-center align-items-center" style="min-height: 80vh;"> <div class="w-50 p-4 border rounded bg-light shadow"> <h2 class="text-center mb-4">Get In Touch</h2> - <form> + + <form method="POST" action="/api/v1/web/opstutorial/contact/submit" enctype="application/x-www-form-urlencoded"> <div class="mb-3"> <label for="name" class="form-label">Name</label> <input type="text" class="form-control" id="name" name="name" placeholder="Insert your name">
