Issue On Validating Text Field Using Custom Validation Function - javascript

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.

Related

TypeError: Cannot find function getSheetById in object Spreadsheet

I want to have an automatic email machine, without having to write all of the messages and email addresses myself.
I'm really new to this, so please don't be too harsh.
function sendOrder() {
var ui = SpreadsheetApp.getUi();
var response = ui.alert('Are you sure you want to send the order?',
ui.ButtonSet.YES_NO);
var deliveryDate = ui.prompt('Delivery date:');
// Process the user's response.
if (response == ui.Button.YES) {
var s = SpreadsheetApp.getActive().getSheetById('1pON34oXVhlpC8goyBxfu6-Gw92tgQBUVUpskZUtgp4E');
var ss = SpreadsheetApp.getActiveSpreadsheet();
var range = ss.getActiveSheet().getDataRange();
var range = s.getRange('A1:C102');
var to = "example#example.com";
var body = '';
var htmlTable = SheetConverter.convertRange2html(range);
var body = "Here is the table:<br/><br/>" +
htmlTable +
"<br/><br/>The end."
MailApp.sendEmail(to, 'Subject', body, {
htmlBody: body
});
};
SpreadsheetApp.getUi().alert('Yeah! Your order has been sent :)');
}
I just expect this to give me a box to enter a date, once the date is entered it should say it has sent and our supplier will see all of the orders.
Issue:
This is because function getSheetById() does not exist.
Solution:
Use openById() instead:
var s = SpreadsheetApp.openById('yourIdHere');
Reference:
openById()
you need to add the function getSheetById(id):
function getSheetById(id) {
return SpreadsheetApp.getActive().getSheets().filter(
function(s) {return s.getSheetId() === id;}
)[0];
}

I want to check the value of fields of two forms

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

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

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;

ajax post data error [object HTMLCollection]

I wrote this script to for a contact form on my website, everything works however instead of storing the data in me database all is get is
[object HTMLCollection] c
an anyone tell me what this is?
or what is going wrong? i have had a look on google but i cant find much information on it.
<script type="text/javascript">
//when the button is clicked
$(document).ready(function() {
$("#button").click(function() {
$('.small').hide();
var name = $("input#name").val();
if (name == "") {
$("span#name").show();
return false;
}
var name = $("input#email").val();
if (name == "") {
$("span#email").show();
return false;
}
var name = $("input#subject").val();
if (name == "") {
$("span#subject").show();
return false;
}
var name = $("textarea#message").val();
if (name == "") {
$("span#message").show();
return false;
}
var dataString = 'name='+ name + '&email=' + email + '&subject=' + subject + '&message=' + message;
$.ajax({
type: "POST",
url: "/scripts/send_message.php",
data: dataString,
});
$("#messagearea").load("console/new_message_profile.php?sent=1");
});
});
</script>
As #Namit mentioned, you use name as a variable everywhere. Building your string, email, subject and message are uninitialised.
They should give you an undefined - but no, due to a weird Internet Explorer behaviour (see Is there a spec that the id of elements should be made global variable?) these variables hold DOM elements. As you seem to have multiple elements with the same id (NEVER DO THAT), here a <span> and an <input>, the variables even seem to hold HTMLCollection objects. Which are casted to the string [object HTMLCollection], when you concat them with other strings.
You're reusing the variable name for all the other fields as well. You need to change the field name to the respective input id.
var name = $("input#email").val(); // needs to be email
var name = $("input#subject").val(); // needs to be subject

Categories

Resources