简体   繁体   中英

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

I'm using EJS for templating in my Node project. I have built a form with over 30 fields. 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. 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. 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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