checking if inputs in a form are set using nodejs

This question already has an answer here:

  • How do I check if an object has a property in JavaScript? 22 answers

  • To get post data, you need to use body-parser.

    Then you need a route handler for handling the post data.

    router.post('/', function (req, res, next) {
        // if you set up body parser correctly, all your form data will be accessible in `req.body`
        console.log(req.body) ; 
    
        const errors = validate(req);
    
        if(errors.length) {
            // show errors
    
            return;
        }
    
        // do something with form data.
    });
    
    function validate(req) {
        const errors  = [];
    
        if (! req.body.firstName) {
            errors.push('First name is required');
        }
    
        // do the same for all input fields
    
        return errors;
    }
    
    链接地址: http://www.djcxy.com/p/27318.html

    上一篇: typeof undefined。 哪个更快更好?

    下一篇: 检查表单中的输入是否使用nodejs设置