Email RegEx Javascript with RPC - javascript

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);
});

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');
}​

Related

Passing Javascript Input Values to a PHP file to Post to SQlite

I wanted to ask how can i get the values of the Javascript Input and store it into a php value so i can post this data into Sqlite3. Im receiving user inputs from the Javascript Prompts. Is there another way to accomplish this also. Any help would be greatly appreciated.
function myFunc(){
var code = prompt("Please enter authorized code twice for security purposes: ");
var email = prompt("Please enter email twice to continue: ");
if(code==""||code==null||code!="1234"){
//Handle Error
window.location.href="error.html";
}
}
document.onreadystatechange = () => {
document.addEventListener('readystatechange', event => {
if (event.target.readyState === "complete") {
myFunc();
}
});
}
Using jquery you can use the $.post method:
function myFunc() {
var code = prompt("Please enter authorized code twice for security purposes: ");
var email = prompt("Please enter email twice to continue: ");
var url = "phpToGetInputs.php";
var data = {
code: code,
email: email
}
$.post(url, data); // "send the data to the php file specified in url"
// code...
}
document.onreadystatechange = () => {
// code...
}
Then, in your PHP file (that you specified as the url)
phpToGetInputs.php:
<?php
if(isset($_POST['email'])) {
$email = $_POST['email']; // get the email input (posted in data variable)
$code = $_POST['code']; // get the code input (posted in data variable)
// do code that requires email and code inputs
}
?>
Use a jQuery post request to send the variable from javascript to php.
$.post([url], { "data" : text });
Look at this website for more information: https://api.jquery.com/jquery.post/

Unable to reload the same page after clicking submit button

I'm making a login page where if the email address already exists i want to stay on the same page and prompt the user that the email address already exists.
Here is the function which is the onClick function that will be called when the button is clicked
function login() {
var username = document.getElementById("username").value;
var pword = document.getElementById("pword").value;
var confpwd = document.getElementById("confpwd").value;
var email = document.getElementById("email").value;
var fname = document.getElementById("fname").value;
var lname = document.getElementById("lname").value;
var gender = document.getElementById("gender").value;
var t = 1;
if (t.toString() !== '0') {
var er = "Email-id already exists";
event.preventDefault();
document.getElementById("nemail").value = er;
document.getElementById("username").value = username;
document.getElementById("pword").value = "";
document.getElementById("confpwd").value = "";
document.getElementById("fname").value = fname;
document.getElementById("lname").value = lname;
document.getElementById("gender").value = gender;
}
}
You must pass a function to the <form onsubmit="return login()">. The login function must return true if you want to submit and false otherwise. See this answer for more details: JavaScript code to stop form submission
Working codepen to illustrate: http://codepen.io/DeividasK/pen/YZqwLO
Depending on how your code is setup to submit. You may just need to insert a return when the code realizes the email address is a duplicate. Something along this path might help prevent the page from moving forward.
if (ListOfExistingEmails.indexof(email) > 0 ) return false;

Meteor custom create user form

Hey guys I am trying to make a custom registration form with the accounts-password package but i get an error in the console : Error invoking Method 'insertUser': Internal server error [500]and on the server side :
=> Meteor server restarted
I20170209-20:25:53.029(1)? Exception while invoking method 'insertUser' ReferenceError: password is not defined
This is my client side code :
Template.Anmeldung.events({
"submit .add-benutzer": function(event){
var Vorname = event.target.Vorname.value;
var Nachname = event.target.Nachname.value;
var Geburtsdatum = event.target.Geburtsdatum.value;
var Email = event.target.Email.value;
var Passwort = event.target.Passwort.value;
Meteor.call('addBenutzer', Vorname, Nachname, Geburtsdatum, Vorname)
Meteor.call('insertUser', Email, Passwort);
event.target.Vorname.value = "";
event.target.Nachname.value = "";
event.target.Geburtsdatum.value = "";
event.target.Email.value = "";
event.target.Passwort.value = "";
FlowRouter.go('/meineEvents');
return false;
}
});
and my server side code :
Meteor.methods({
insertUser(emailVar, paswordVar){
Accounts.createUser(emailVar, passwordVar);
}
});
Thank you for every help ;)
It is just that you use the Accounts.createUser wrong, it should be this:
Meteor.methods({
insertUser(emailVar, paswordVar){
Accounts.createUser({
email: emailVar,
password: passwordVar
});
}
});
You don't need to call Accounts.createUser on the server in a Meteor method. In fact, doing so is dangerous because you are passing the password from the client to the server without encrypting it.
I recommend you read the docs for the functions you use: Link
In this case you can just use do
Template.Anmeldung.events({
"submit .add-benutzer": function(event){
var Vorname = event.target.Vorname.value;
var Nachname = event.target.Nachname.value;
var Geburtsdatum = event.target.Geburtsdatum.value;
var Email = event.target.Email.value;
var Passwort = event.target.Passwort.value;
Meteor.call('addBenutzer', Vorname, Nachname, Geburtsdatum, Vorname)
Accounts.createUser({ email: Email, password: Passwort} , (error) => {
if(error) {
alert(error.reason);
}
});
[...]
}
});
As a side note, in some cases you need to create a user on the server. In these cases you can use the Accounts._hashPassword() on the client before sending the password to the server. This ensures the password isn't sent in plain text.

How to use onUrlChanged?

I'm trying to log in into gmail account using javascript, and I have a problem:
after I insert my email and press 'next', the page redirect me to new url asking for my password. my question is, How can I monitor the current url and know when it changes? Im trying to use page.onUrlChanged but it doesn't work
test.open(url, function(status) {
test.page.evaluate(function (email, password) {
document.getElementById('Email').value = email;
document.getElementById('next').click();
test.page.onUrlChanged = function(targetUrl) {
console.log(targetUrl);
}
}, email, password);});
you can use phatomjs for log URL change..below is simple code for that
var webPage = require('webpage');
var page = webPage.create();
page.onUrlChanged = function(targetUrl) {
console.log('New URL: ' + targetUrl);
};
find more about phantomjs
http://phantomjs.org/api/webpage/handler/on-url-changed.html
or
You can use
window.onblur = function() { console.log('blur'); }

javascript onBlur not working, and how to connect javascript files

I have two javascript files that I am using to validate an email address.
validate.js:
function checkEmail(userEmail) {
var email = userEmail
var emailFilter = /^([a-zA-Z0-9_\.\-])+\#(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (emailFilter.test(email.value)) {
//alert('Please provide a valid email address');
//email.focus;
return true;
}
else{
return false
}
}
navigation.js EDIT:
$(document).ready(function() {
//ADDED IMPORTS
var imported = document.createElement('script');
imported.src = 'lib/validation.js';
document.head.appendChild(imported);
console.log("DOCUMENT IS READY!");
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() {
var sEmail = $('#usernamefield').val();
if ($.trim(sEmail).length == 0) {
alert('Please enter valid email address');
e.preventDefault();
}
if (checkEmail(sEmail)) {
alert('Email is valid');
}
else {
alert('Invalid Email Address');
e.preventDefault();
}
});
...(more code follows but not relevant)
I am also using this jade template:
login.jade:
form(action="")
key EMAIL
input(type="text", name="username", id="usernamefield")
p hello world
br
key PASSWORD
input(type="text", name="password", id="passwordfield")
p hello world
br
input(type="submit", name="loginButton", id="login-button", value="LOGIN")
My issue is that when I input something into my email field, I do not get an alert message in any case. Am I allowed to just have to separate javascript files and call the methods I defined in validate.js within navigation.js? I tried putting the validate.js code in navigation.js, but even then it did not work. I would like to keep the files separate. Am I missing something obvious? I want it so that once the user inputs the email, and leaves the field, a message should appear warning if the email is valid or not.
Your help is appreciated.
Is it the blur Event or the checkEmail the problem? try to put a alert() or console.log() just after your blur (and make sure to lose focus on your input). Seperate file shouldn't be a problem. And also have you check for errors in your console ?
JavaScript string has no "value" field
After
var sEmail = $('#username').val();
sEmail becomes a string.
You are passing this string to checkEmail method and try to get "value" from a string:
if(!emailFilter.test(email.value)) {//...}
Replace to
if (!emailFilter.test(email)) {//...}
You are already sending the value of email into checkemail function. So in checkEmail function in validate.js remove email.value in second line of function checkEmail
function checkEmail(userEmail) {
var 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 false;
}
}

Categories

Resources