hi i am trying to dynamically validate my registration form, so that check are being made, when the user enters values into input fields using jquery and php. i am returning the values using a json array, the first input box is validated but the second one does not get validated and im not sure why? any help would be mostly appreciated.
This problem is occurring because your validate() function takes two parameters: fname and name, but you are always only sending the first parameter and not the other one.
To fix it, in your fname events do this:
validate($('#fname').val() , "");
--------------- ---
| |
fname lname
and in your lname events do this:
validate("" , $('#lname').val());
--- ----------------
| |
fname lname
and in your PHP make sure you set it so that it reads no "blanks":
if( isset($_POST['lname']) && $_POST['lname'] !== "")
if( isset($_POST['fname']) && $_POST['fname'] !== "")
Related
I would like to replace the null record with N/A when possible in JavaScript.
I have the form, where some of the questions are reliant on other questions. If, for instance, some of the questions in marked as "yes" then other questions are not required, therefore they're inactive, as per below.
Next, I've prepared the email form like shown here:
HTML Assigning the checkbox to the form action already defined
and everywhere, where I have the unanswered question (while inactive ones) the result shows null.
I would like to have N/A instead of null everywhere, where questions are to be inactive due to selection.
My initial code looks like this:
var surveyFailure = formData.get('h3g_survey_failure_reason');
and I tried something like this:
var surveyFailure = if
formData.get('h3g_survey_failure_reason') = null {
surveyFailure = "N/A"
};
but it doesn't work, as i get
Uncaught SyntaxError: Unexpected token 'if'
Is there any chance to replace the null with N/A?
Full code is available here:
https://jsfiddle.net/r5kw4f6g/
You have a syntax error, the correct code shoud be this:
var data = formData.get('h3g_survey_failure_reason');
var surveyFailure = (data === null) ? "N/A" : data;
I am trying to build a Gmail database for all users in our company, I want to get these Gmails through piece of code and apply an if condition on them to see if they match or not, but I am not successful so far. I don't know if it's because the string I retrieve can't be read as an apps script syntax or it's because I am doing it wrong. here is my code:
I put all needed Gmails in 1 cell (B3) like this --->
(user == 'abc1#gmail.com') || (user == 'abc2#gmail.com')
//my code
var user = Session.getEffectiveUser();
var mailDB = SpreadsheetApp.openById('Sheet ID').getSheetByName('Sheet Name'); //mails database
var cellcondition = HeadOfficeMailDB.getRange("A3"); // cell with certain value 22
var Gmails = mailDB.getRange("B3").getValue(); //retrieve Gmails in the cell as one whole string
if((Gmails) && (cellcondition == 22))
{
var newcell = HeadOfficeMailDB.getRange("C3").setValue(4);
}
I set the Database with 2 different Gmails, but when I run the code with a third Gmail not included in Database, it runs anyway. it seems like it doesn't recognize the string as a syntax, or am I doing something wrong?
kindly if you have any fix or recommendations or other better ideas to handle such issue please don't hesitate to provide me with your assistance immediately.
The following line
var cellcondition = HeadOfficeMailDB.getRange("A3");
assigns a Class Range objec to cellcondition. Replace it by
var cellcondition = HeadOfficeMailDB.getRange("A3").getValue();
to assign the value of A3 to cellcondition.
To evaluate the value of Sheet Name!B3 ((user == 'abc1#gmail.com') || (user == 'abc2#gmail.com')) you could use eval() but doing this is an enormous security risk. It's better to store the email address as a list (separated by using a separator like a comma) then use String.prototype.split() and Array.prototype.index() or create a Set object.
Related
javascript pass eval variables
I'm using Jquery Framework, I'm trying to validate input data, which is not equal to null or empty string. My approach is to validate data in a single line as I'm maintaining cookies storage. My question is, how can I validate input text that value its not an empty string, similarly I checked radio buttons and check boxes that are checked. How can I validate input[type='text'] with same condition input[value !=''] in a single line.
Here's my code:
_strFormElements = "input[type='text']," +
"input[type='checkbox']:checked," +
"input[type='radio']:checked," +
"input[type='image']," +
"input[type='file']," +
"select," +
"textarea";
I then checking as follow, but I want to check and validate before theses checks as my init method creates empty feilds before this.
if (elem.is('input:text') || elem.is('input:hidden') || elem.is('input:image') ||
elem.is('input:file') || elem.is('textarea')) {
elem.val(value);
}
My Try:
I've tried input[type='text' value != ''] but I'm unable to check this in a single line.
I'm designing a shopping cart system for my site. I have an input to enter the quantity for each item, with the value 1 automatically put in. The problem is, if someone clicks in the input and deletes the 1, leaving the field blank, then the item isn't added to the cart.
How can I set a 1 in the input field if it is blank, either using js or jquery? Or if the field is blank have the form automatically submit a 1?
Something like this should work:
$("#input_ID").blur(function() {
if($.trim($(this).val()) === "") {
$(this).val("1");
}
});
If it needs to be a group of inputs, simply give them a common class and change:
$("#input_ID").blur( . . .
. . . to:
$(".class_name").blur( . . .
var value = $(your_input).val();
if ( value == '' ) value = 1
The following solution is only js no jquery.
Within your internal function, wherever you are saving the input.
For example:
let itemQuantity = document.getElementById("item_quantity");
//you can also add account for space or other non numeric values (consider looking into regex)
if (itemQuantity === ""){
itemQuantity = 1
// or "1" depending on how you need to pass the values
return taskPriority;
}
alternate:
if (itemQuantity.value === ""){
itemQuantity = 1
// or "1" depending on how you need to pass the values
}
The first one will force the user to reset default to 1 and then add to cart again.
Second will by default set it to 1 if they input blank values.
that should allow you to set a default value.
I only want to execure a ceratin code IF three input fields do not have empty values. So why doesn't this code work:
if($("#field1").val() != "" && $("#field2").val() != "" && $("#field3").val() != "")
Make sure the input fields are named properly? They should have the id attribute for them set.
Do all three inputs have ids matching your selectors? You may also want to check that the type of the values is not undefined like so:
if(typeof($('#field1').val())!='undefined' ...