5

So I've been trying to research online for solutions, and none of them seem to work either because I'm doing it wrong, or they don't work in my situation.

I have a webpage which gives the user a place to enter email/pass etc. When they press the submit, it calls a post function, which has all the validation contained within it. Like so:

app.post('/check', function(req, res){
    function emailCheck(email){
        //when theres an error: console.log(error), and return false
    }

    function passCheck(password){
        //when theres an error: console.log(error), and return false
    }

    if (passCheck == true && emailCheck == true){
        //enter user into database
    }
}

When there is an error, I want it to be displayed to the user, either by use of popup box, or just text positioned under the sign up box.

Any suggestions would be great.

4
  • 1
    what view engine are you using? Commented Sep 15, 2015 at 18:19
  • @Alex All I'm using at the moment is HTML and node.js. Not sure if that's what you wanted? Commented Sep 15, 2015 at 18:25
  • Do you using some framework (Express, etc)? What error you could catch? With express, it's possible catch some http errors, as 404 or 502, in each request... Commented Sep 15, 2015 at 18:35
  • @LucasCosta Oh, yes, I'm using express. Commented Sep 15, 2015 at 18:37

2 Answers 2

2

I would make an ajax request from the client to the server. If the validation fails you can send data specifying the problem to the client and do what you'd like with it.

Sign up to request clarification or add additional context in comments.

3 Comments

Would this then require me making all the validation on the client side, rather than using the server?
No, an example would have the server do the validation logic and return what fields are incorrect to the client and the client can process that information.
Sorry for late reply, can you point me in the right direction? I've been researching of how to do this through AJAX but can't find anything.
2
   //register User on post request from register form
router.post("/register", function(req, res){
    var newUser = new User({username: req.body.username});
    User.register(newUser, req.body.password, function(err, user){
        if(err){
           // console.log(err.message);
            req.flash("error", err.message);
            return res.render("register");
        }
        passport.authenticate("local")(req, res, function(){
             req.flash("success","You created a new User Account " +user.username);
           res.redirect("/campgrounds"); 
        });
    });
});

you can Install: npm install flash-connect

and use error.message in console log to display the errors

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.