简体   繁体   English

如何检查空字段并返回输入数据的表单

[英]How to check for empty fields and return to the form with entered data

I'm using EJS for templating in my Node project.我在我的 Node 项目中使用 EJS 进行模板化。 I have built a form with over 30 fields.我已经建立了一个包含 30 多个字段的表单。 On submit I check for empty and required form fields and return to the form if found empty.在提交时,我会检查空的和必填的表单字段,如果发现是空的,则返回表单。 I want to save other form fields data entered by the user, so they will not have to re-enter everything from the start.我想保存用户输入的其他表单字段数据,这样他们就不必从一开始就重新输入所有内容。 This is what my code looks like and I didn't add all the field names here to save time reading it.这就是我的代码的样子,我没有在此处添加所有字段名称以节省阅读时间。

const register = (req, res) => {
    const firstname = req.body.firstname;
    const lastname = req.body.lastname;
    const fullname = req.body.fullname;
    const nic = req.body.nic;
    const gender = req.body.gender;
    const dob = req.body.dob;
    const age = req.body.age;
    if (!firstname || !lastname || !nic) return res.redirect('/users/register');
}

I'm very new in coding and stackoverflow, so please help me solving this.我在编码和stackoverflow方面很新,所以请帮我解决这个问题。 Many thanks in advance.... Excuse me for my not good enough english非常感谢....请原谅我的英语不够好

Just add a onkeydown to the inputs and check if the input is empty.只需在输入中添加一个 onkeydown 并检查输入是否为空。 If it is, alert a message to the user otherwise, continue.如果是,则向用户发出一条消息,否则,继续。 Here is the code:这是代码:

<input type="text" id="input" onkeydown="myFunction()">
<script>
function myFunction(){
var a = document.getElementById('input').value;
if(a == ""){
alert("Please enter something"); 
}
else{
// code...
}
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM