Is it possible to validate form on client and server only with Javascript?

Asked

Viewed 591 times

-1

In the particular case, I would like to create a form contact, with name, type of service (dropdown menu: webdesign, mobile), subject and message. I would also like to send this form with name, type of service and message for a specific email in text format.

Usually the validation of a form is done on the server with ASP, Java. Now, would it be possible to validate only with Javascript? What technology is required to do this?

  • The javascript part had done at the bottom but the sending of the email always has to be done on the server side. With php/ASP etc..

  • Node is server, Angularjs is not

  • 1

    I do, but do you want to do the client-side validations in javascript? (as I did below) Or the server-side... Only the server side that can send emails, be it in Node/php/etc..

  • 3

    See http://answall.com/questions/102522/%C3%89-poss%C3%Advel-fazer-autentica%C3%A7%C3%A3o-valida%C3%A7%C3%A3o-de-formul%C3%A1rio-com-Node-js-puro? Rq=1 can be q help. And this https://nodemailer.com/

  • 6

    http://answall.com/q/13298/101

  • Paul, see this plugin for jquery : https://jqueryvalidation.org

Show 1 more comment

1 answer

4

You can use Client-Side validation with Javascript (pure or using some biliotec like Jquery, Angularjs, etc). This will occur before you send the data to the server. Something like this:

function Valida() {
    if (document.getElementById("nome").value.length < 1) {
        alert("Digite um nome.");
        document.getElementById("nome").focus();
        return false;
    } else if (document.getElementById("idade").value.length < 1) {
        alert("Digite uma idade.");
        document.getElementById("idade").focus();
        return false;
    } else if (document.getElementById("sexo").value == 0) {
        alert("Escolha um sexo.");
        document.getElementById("sexo").focus();
        return false;
    } else {
        return true;
    }
}

Source: https://bytecrow.wordpress.com/2011/10/17/tutorial-para-iniciantes-05-validacao-de-formulario-com-javascript/

You can (and recommend) validate also on the server side using Nodejs and Javascript.

The code is big, see in https://udgwebdev.com/trabalhando-com-validators-no-node-js.

You can send txt or html emails using nodemailer, plugin for express:

var nodemailer = require('nodemailer');

// create reusable transporter object using the default SMTP transport
var transporter = nodemailer.createTransport('smtps://user%40gmail.com:[email protected]');

// setup e-mail data with unicode symbols
var mailOptions = {
    from: '"Fred Foo " <[email protected]>', // sender address
    to: '[email protected], [email protected]', // list of receivers
    subject: 'Hello ✔', // Subject line
    text: 'Hello world ', // plaintext body
    html: '<b>Hello world </b>' // html body
};

// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
    if(error){
        return console.log(error);
    }
    console.log('Message sent: ' + info.response);
});

Source: https://github.com/nodemailer/nodemailer

  • 1

    What would happen if JS were disabled in my browser?

  • @Magichat the sky would fall on Earth.

  • @Nan hehe, seriously speaking...

  • I would send the form normally, regardless of whether to enter (or not) any information in the fields. The question the bigown linked in the comments is a good one: Using client validation is sufficient?

  • I suspected from the beginning....

  • If Javascript is disabled, the browser can send the data (if it is not using ajax). On the server side everything will occur normally. What will not happen is validation in the client-side.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.