user2615699
user2615699

Reputation:

Email validation using nodeJS/express/ and ajax

I am working on a web app and I am trying to do an email/ password verification so that right after the user enters something into the email field, they will be told if their email is correct or not. I am using angular/express and jade, but I cannot seem to get the error messages to display. I think the error is with the connections between my routers, but I am not sure.

I have a validation.js file:

 var email; 
 var password; 
 var INVALID_EMAIL = "The email you entered is invalid"

exports.validateEmail = function (userEmail) {

    email = userEmail
    var emailFilter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

    if (emailFilter.test(email)) {
    //alert('Please provide a valid email address');
        email.focus;
        return true;
    }
    else{
        return false
    }
}

I also have a javascript file called navigation.js where I make the calls to post the message:

$(document).ready(function() {

    var viewsWrapper = $("#views-wrapper");
    var loginButton = $("#login-button");
    var registerButton = $("#register-button");

    // Login Link
    // TODO: Unclear if needed
    $("ul li.login").click(function() {
        $.get('/login', function(data) {
            viewsWrapper.html(data);
        });
    });

$('#usernamefield').blur(function() {
    e.preventDefault(); 

    $.post('/login', INVALID_EMAIL, function(data) {
        $(".emailErrorMessage").html(data); 
    });
}

... more code but not relevant Here I am making a jQuery post call, and trying to change a paragraph in my jade template for the login screen. The paragraph has a class of emailErrorMessage

login.jade:

form(action="")
key EMAIL
    input(type="text", name="username", id="usernamefield")
p 
    span(class='emailErrorMessage') 
br
input(type="submit", name="loginButton", id="login-button", value="LOGIN")

And finally in my app.js I make a call:

app.post('/validate/email', validation.validateEmail);

Am I connecting these things together properly? I have a feeling I am missing something but I cannot put my finger on it. I know it is a lot of code, but anything helps. I am trying to make it so that once the user inputs their email, and leaves int email input box, they get a message saying invalid email if their email is incorrect. Im, not sure I'm using the post call correctly.

Upvotes: 0

Views: 2871

Answers (1)

Sabbir
Sabbir

Reputation: 359

It looks like you should try out : https://github.com/ctavan/express-validator https://npmjs.org/package/express-validator

In your app.js :

var ev = require('express-validator');

And use the module:

app.use(express.urlencoded());
app.use(ev());
// Order is important.

Make a middleware and pass the error object :

 function validator(req, res, next) {

    req.checkBody('email', 'Invalid Email').isEmail();
    req.checkBody('password', 'Invalid Password ! Mustbe Between 6-20 Charctersters').len(6, 20);

    var errors = req.validationErrors(true);
    if (errors) {
            res.render('register', { errors: errors});
        }
    } else {
        next();
    }
};

Setup route using this middleware :

app.post('/register', validator, otherStuff);

In jade template you can loop through error object and display error messages to the client.

Upvotes: 2

Related Questions