I want to check the value of fields of two forms - javascript

I have an application, in the app there two forms which are about contact info, each form in different tab; users need to fill. What I want to do is check if the value of the first form values equal the second form. The below is what I did.
What do you think about this way is efficient? If not or have a better way, please share with me.
// form 1
var fName = $("#FirstName").val();
var lName = $("#LastName").val();
var street = $("#Street").val();
var city = $("#City").val();
var cellPhone = $("#cellPhone").val();
var email = $('#Email').val();
// form2 of the main contact
var fName2 = $("#FirstName2").val();
var lName2 = $("#LastName2").val();
var street2 = $("#Street2").val();
var city2 = $("#City2").val();
var cellPhone2 = $("#cellPhone2").val();
var email2 = $('#Email2').val();
//actually i have along "if"
if(fName==fName2 && lName == lName2 && street==street2 && city==city2 && cellPhone==cellPhone2 && email==email2){
}
else {
}

var form1 = {
firstName: $("#FirstName").val(),
lastName: $("#LastName").val(),
street: $("#Street").val(),
city: $("#City").val(),
cellPhone: $("#cellPhone").val(),
email: $('#Email').val()
}
var form2 = {
firstName: $("#FirstName2").val(),
lastName: $("#LastName2").val(),
street: $("#Street2").val(),
city: $("#City2").val(),
cellPhone: $("#cellPhone2").val(),
email: $('#Email2').val()
}
for (var k in form1) {
if (form1[k] === form2[k]) return false
}
// your code
Doing so makes it easier for you to add fields.
And I think collecting these data can also be done with a loop.

You can use Jquery form validation plugin. Its simple to use and offers lot of validation methods. You can also code your custom methods
https://jqueryvalidation.org/

Since your are using jquery you can use the $.submit() method. This allows you to run additional functions before the form is submitted. you can simply:
$('#someForm1').submit(function (e) {
var val1 = $(this).find('.inputval1').val();
var val2 = $('#someForm2').find('.inputval1').val();
if (val1 === val2) {
return;
} else {
e.preventDefault();
}
});

Related

How to update data without deleting anything else?

I need to set a value on my Firebase realtime-database.
Here is my database organization:
Users:
U20180422:
ID: U20180422
Name: "Jason"
Surname: "Smith"
Address: "4198 Norma Avenue"
Age: "30"
creation date: "04/22/2018"
U20180311: ...
U20180304: ...
U20180215: ...
...
I make this snippet for write and it works:
<script language="javascript">
//this retrieves from the form of registration the ID of the user
var userId = document.getElementById("searchfield").value;
// Initialize Firebase
var config = {
//my configurations
};
firebase.initializeApp(config);
console.log(firebase);
var database = firebase.database();
var ref = database.ref('Users/' + userId);
var data = {
Account_validated = "OK"
}
ref.set(data)
</script>
But in this way the data that were present are deleted and only account_validated state is present in the path. So I thought I had to first retrieve all the data already in possession and then send them to the database along with the new data.
This is what is not working in my code:
//Retrieve from database
ref.on('value', gotData, errData);
function gotData(data) {
var value = data.val();
var getvalue = Object.value(value);
for (var i=0; i < value.lenght; i++) {
var k = value[i];
var name= value[k].Name;
var surname= value[k].Surname;
var address= value[k].Address;
var age= value[k].Age;
var creation_account_date= value[k].creation date;
console.log(name, surname, address, age, creation date);
}
}
function errData(err) {
console.log('Error!');
console.log(err);
}
{
snapshot.forEach(function(child) {
var childData = child.val();
var name=child.val().Name;
var surname=child.val().Surname;
var address=child.val().Address;
var age=child.val().Age;
var creation_account_date=child.val().creation date
});
});
//Write
var data = {
ID: userId,
Name: name,
Surname: surname,
Address: address,
Age: age,
creation date: creation_account_date
}
ref.set(data)
To update only the keys that you specify in your data, use update:
var data = {
Account_validated: "OK"
}
ref.update(data)
This will update only the Account_validated key under ref, and leave other child properties unmodified.
What errors are you seeing?
At first glance you've got a typo in your for loop:
for (var i=0; i < value.lenght; i++)
You've misspelt length so value.lenght will resolve to undefined and execution will just skip over the loop.

Creating users in Google Apps from Apps Script

I am using Google Apps Script to create users in Google Apps in bulk via spreadsheet
my spreadsheet has 4 columns where i have firstname, lastname, emailId, and password, as these are required to create users.
My following script works fine and create users without issue, however it stops at a point if any user already exists, and does not move forward to attempt other ones in spreadsheet.
I want it to loop through the whole spreadsheet, skip the ones that already exists, and create the ones who don't
Any help is appreciated.
function createUsers() {
var ss = SpreadsheetApp.getActive()
var sheet = ss.getSheetByName("Create Users")
var values = sheet.getDataRange().getValues()
for(i=1; i <values.length; i++)
{
var fName = values[i][0]
var lName = values[i][1]
var email = values[i][2]
var password = values[i][3]
var status = values[i][4]
var user = AdminDirectory.Users.insert({
"primaryEmail": email,
"password": password,
"orgUnitPath": "/MDM Testing",
"name": {
"familyName": lName,
"givenName": fName
}
})
Logger.log(user);
}}
You can add If condition to check duplicate user as follows.
function createUsers() {
var ss = SpreadsheetApp.getActive()
var sheet = ss.getSheetByName("Create Users")
var values = sheet.getDataRange().getValues()
for(i=1; i <values.length; i++)
{
var fName = values[i][0]
var lName = values[i][1]
var email = values[i][2]
var password = values[i][3]
var status = values[i][4]
// Check user exist or not by email address.
var userFromDirectory = AdminDirectory.Users.get(email);
if(email != userFromDirectory.primaryEmail){
var user = AdminDirectory.Users.insert({
"primaryEmail": email,
"password": password,
"orgUnitPath": "/MDM Testing",
"name": {
"familyName": lName,
"givenName": fName
}
})
Logger.log(user);
} // end of if
}}
Use
var userFromDirectory = AdminDirectory.Users.list.name
instead of
var userFromDirectory = AdminDirectory.Users.get(email);

trying to create form validation

trying to create form validation to validate so that only alphanumeric characters will be allowed to submit using javascript but nothings working and i dont know what to do next, no jquery just plain js
function validate() {
'use strict';
var errMsg = "";
var result = true;
var fname = document.getElementById('fname').value;
if (!fname.match(/^[a-zA-Z]+$/)) {
errMsg = errMsg + "Alphanumeric Characters only\n";
result = false;
}
if (errMsg !== "") {
alert(errMsg);
}
return result;
}
function init() {
var enquiries = document.getElementById('enquiries').value;
enquiries.onsubmit = validate;
}
window.onload = init;
var enquiries = document.getElementById('enquiries').value;
enquiries.onsubmit = validate;
.onsubmit is being assigned to the value of the enquiries element, rather than the element itself. If you remove .value, your code should work.
plunker: http://plnkr.co/edit/SL7lZzLSje7BEuDRttym?p=preview

Issue On Validating Text Field Using Custom Validation Function

Can you please take a look at This Demo and let me know what I am doing wrong in this Custom Function to validate the Text input?
$(function () {
var fname = $('#fname').val();
var lname = $('#lname').val();
var proceed = true;
function nameInput(inputData) {
var textBox = $.trim($(inputData).val())
if (textBox == "") {
alert('Field Can Not be Empty');
}
}
$("#pro").on("click", function (e) {
nameInput(fname);
e.preventDefault();
});
});
apparently the nameInput() is returning Field Can Not be Empty in both empty and filled format of the fname input. Thanks
You need to remove the two calls to val() when you declare your field variables:
var fname = $('#fname');
var lname = $('#lname');
As it is, you're passing the value to your method, and then in your method calling val() again.

Meteor insert record using form values

First of all, I'm a complete JS novice. I'm experimenting with Meteor.My objective is to to build a simple form that inserts records into a table. I've set up variables to grab values from each input, and I've placed those variables into an insert method. When I click the button, it recognizes the click, but doesn't pull any values from the inputs. I'm sure I'm missing something simple here, I just can't figure out what it is.
Here's the JS:
var Leads = new Meteor.Collection("Leads");
if (Meteor.is_client) {
Template.Leads.LeadsArr = function(){
return Leads.find();
};
Template.AddLeads.events = {
"click input.submit" : function () {
var name = document.getElementById('input#name').value();
var email = document.getElementById('#email').value();
var type = document.getElementById('#type').value();
var date = document.getElementById('#date').value();
var message = document.getElementById('#message').value();
Leads.insert({leadName : name, leadEmail : email, leadType : type, leadDate : date, leadComment : message});
}
};
} // end is_client
document.getElementById expect the id, not the selector. Also, value is a property of an input, not a function. So your input queries should be like this.
var name = document.getElementById('name').value;
var email = document.getElementById('email').value;
var type = document.getElementById('type').value;
var date = document.getElementById('date').value;
var message = document.getElementById('message').value;

Categories

Resources