Search code examples
javascriptjqueryregexrpc

Email RegEx Javascript with RPC


I have a few codes here of js. And I wanted to put a validation for email and prevents user from submitting a form when it is not valid. Please bear with me since I am new with this technology. Thank you for understanding :)

var session = require('web.session');
var db_name = $("input[name='partner_name']").val();
var contact = $("input[name='contact_name']").val();
var email = $("input[name='email_from']").val();
var url = '/page/website_crm.contactus_thanks';


 $(function(){
        $("#start_trial").click(function(){ // no idea how to put email validation here
                    session.rpc('/custom/createaccount', {                      
                            db_name : db_name, 
                            contact_name: contact, 
                            email_from: email
                        });
                    }).then(function () {
                        console.log("Creating user account")
                    });
                    
                   // that won't proceed here
                    setTimeout(function (){
                       window.location = url;
                    }, 5000);

});

Solution

  • if you want to test if email is ok, lot of solution, this pattern does the job:

    var email = $("input[name='email_from']").val();
    var pattern = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/;
    
    if(!pattern.test(email))
    {
      alert('e-mail address is not valid');
    }​