使用谷歌HTML服务上传文件时验证HTML输入字段
Validating HTML Input fields when uploading file using google HTML Service
我今天大部分时间都在处理这个问题,似乎无法解决我的问题。
我在网上发现了一个使用.gs、html和javascript的脚本,可以将文件上传到谷歌表单。但问题是,我正在尝试修改表单,以确保上传文件的个人也包括他们的姓名和电子邮件。
我在下面包含我的代码。
本质上,在我调用提交按钮后,我无法获得检查2个给定字段的代码。你能提供一些建议吗?如果我取出"check",程序就会运行。但是,我无法获得正确的代码来检查字段是否已填写。
提前谢谢。
/* The script is deployed as a web app and renders the form */
function doGet(e) {
return HtmlService.createHtmlOutputFromFile('form.html')
.setSandboxMode(HtmlService.SandboxMode.NATIVE);
// This is important as file upload fail in IFRAME Sandbox mode.
}
/* This function will process the submitted form */
function uploadFiles(form) {
try {
/* Name of the Drive folder where the files should be saved */
var dropbox = form.myName + "Design request form folder" + form.myEmail;
var folder, folders = DriveApp.getFoldersByName(dropbox);
/* Find the folder, create if the folder does not exist */
if (folders.hasNext()) {
folder = folders.next();
} else {
folder = DriveApp.createFolder(dropbox);
}
/* Get the file uploaded though the form as a blob */
var blob = form.myFile;
var file = folder.createFile(blob);
/* Set the file description as the name of the uploader */
file.setDescription("Uploaded by " + form.myName);
/* Return the download URL of the file once its on Google Drive */
return "File uploaded successfully " + file.getUrl();
} catch (error) {
/* If there's an error, show the error message */
return error.toString();
}
}
<!-- Include the Google CSS package -->
<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons.css">
<!-- You can also include your own CSS styles -->
<style>
form { margin: 40px auto; }
input { display:inline-block; margin: 20px; }
</style>
<script>
// The function will be called after the form is submitted
function uploadFile() {
var x=document.coForm.fieldName.value;
if (x == null || x ==""){
alert(x);
return false;}
document.getElementById('uploadFile').value = "Uploading File..";
google.script.run
.withSuccessHandler(fileUploaded)
.uploadFiles(document.getElementById("coForm"));
return false;
}
// This function will be called after the Google Script has executed
function fileUploaded(status) {
document.getElementById('coForm').style.display = 'none';
document.getElementById('output').innerHTML = status;
}
</script>
<!-- This is the HTML form -->
<form id="coForm" name="coForm">
<!-- Text input fields -->
<input type="text" class="required" id="fieldName" name="myName" placeholder="Your name..">
<input type="email" class="required" id="fieldEmail" name="myEmail" placeholder="Your email..">
<!-- File input filed -->
<input type="file" name="myFile">
<!-- The submit button. It calls the server side function uploadfiles() on click -->
<input type="submit" id="uploadFile" value="Upload File" onsubmit="uploadFile();">
</form>
<!-- Here the results of the form submission will be displayed -->
<div id="output"></div>
将"提交"按钮设置为常规按钮,并将属性更改为onmouseup
:
<input type="button" id="uploadFile" value="Upload File" onmouseup="uploadFile();">
从输入字段中删除class="required"
:
<!-- Text input fields -->
<input type="text" id="fieldName" name="myName" placeholder="Your name..">
<input type="email" id="fieldEmail" name="myEmail" placeholder="Your email..">
将此代码添加到uploadFile()
函数:
function uploadFile() {
var name = document.getElementById('fieldName').value;
var email = document.getElementById('fieldEmail').value;
console.log('name: ' + name);
console.log('email: ' + email);
if (name === "" || email === "") {
alert("Your name and/or email is missing!");
return;
};
注意console.log()
语句。它们将信息打印到浏览器控制台日志中。要打开浏览器日志,请按f12键,(对于Chrome和其他一些浏览器)
相关文章:
- 为什么我的 JavaScript 会导致 HTML 验证错误
- 检查浏览器是否支持 HTML 验证
- 使用EJS模板进行HTML验证;文本/模板”;元素
- 通过HTML验证(RegEx)在电子邮件中验证域
- 如何使用PHP和HTML验证表单
- 如果我使用类型 = 按钮,HTML 5 验证就会消失
- 如何使用 HTML 验证日期范围
- HTML 5 验证表单缺少操作
- 如何隐藏启用 JavaScript 的浏览器的验证码并通过 html 验证
- 单击按钮时手动触发HTML验证
- 正在禁用HTML验证.如何设置'novalidate'适用于全球范围内的每种形式
- 使用Linkedin代码登录会给我html验证错误
- ajax在html验证后提交
- 如何通过使用OnChange Javascript HTML验证表单
- Grunt html验证插件
- 在不破坏HTML验证的情况下换行表行
- 哪个事件导致浏览器显示HTML验证消息
- javascript中的HTML验证
- 在我的textArea中的W3C Html验证器
- Piwik跟踪代码破坏HTML验证