簡體   English   中英

如何檢查空字段並返回輸入數據的表單

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

我在我的 Node 項目中使用 EJS 進行模板化。 我已經建立了一個包含 30 多個字段的表單。 在提交時,我會檢查空的和必填的表單字段,如果發現是空的,則返回表單。 我想保存用戶輸入的其他表單字段數據,這樣他們就不必從一開始就重新輸入所有內容。 這就是我的代碼的樣子,我沒有在此處添加所有字段名稱以節省閱讀時間。

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');
}

我在編碼和stackoverflow方面很新,所以請幫我解決這個問題。 非常感謝....請原諒我的英語不夠好

只需在輸入中添加一個 onkeydown 並檢查輸入是否為空。 如果是,則向用戶發出一條消息,否則,繼續。 這是代碼:

<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