I have been studying the if and else if statements and trying to implement them on a project.
I was able to apply it to a single field on my form but had no success while trying to apply it to multiple fields.
I hope that I can find someone with more understanding to take a look and help me understand better what I do not understand.
Ok, so what I am trying to achieve is a validation of user input on my form.
I use AppleScript for the form and submission is made to the spreadsheet but I am trying to use the if and else if statements to check if a user is providing information that is already recorded on the spreadsheet to prevent multiple inputs.
I do not know if I have to provide the entire code of my app script but,
-1, -5, -6, and -7 stand for the row numbers in the spreadsheet where the user inputs are saved.
(-1 USERNAME), (-5 ID NUMBER), (-6 PHONE NUMBER), (-7 EMAIL).
My script checks if user input does not match with any record in these rows on the spreadsheet and rejects it if there is a match and asks them to provide another one.
To check a single field, I use the code below and it works fine:
google.script.run
.withSuccessHandler(checkResult => {
if(arrInputSignUp.every(d => d.value == "")){
document.getElementById('loadingData').classList.toggle('invisible');
sweetAlert("","Fill every fields!!", "error");
}else if(arrInputSignUp.some(d => d.value == "")){
document.getElementById('loadingData').classList.toggle('invisible');
sweetAlert("","Fill all the fields!!", "warning");
}else if(arrInputSignUp.every(d => d.value !== "") && checkResult > -1 ){
document.getElementById('loadingData').classList.toggle('invisible');
sweetAlert("","Username taken. Enter a different one!!", "warning");
event.preventDefault();
}
else (arrInputSignUp.every(d => d.value !== "") && checkResult == -1 )
{
google.script.run
.withSuccessHandler( () => {
document.getElementById('loadingData').classList.toggle('invisible');
sweetAlert("","Data Saved!!", "success");
// login();
window.open(<?= scriptURL ?>, '_top');
} )
.saveSignUp(objInput);
};
})
.checkNewLabel(inputNewKeyword.value);
};
To check the other fields, I use the below code but does not work:
google.script.run
.withSuccessHandler(checkResult => {
if(arrInputSignUp.every(d => d.value == "")){
document.getElementById('loadingData').classList.toggle('invisible');
sweetAlert("","Fill every fields!!", "error");
}else if(arrInputSignUp.some(d => d.value == "")){
document.getElementById('loadingData').classList.toggle('invisible');
sweetAlert("","Fill all the fields!!", "warning");
}else if(arrInputSignUp.every(d => d.value !== "") && checkResult > -1 ){
document.getElementById('loadingData').classList.toggle('invisible');
sweetAlert("","Username taken. Enter a different one!!", "warning");
event.preventDefault();
}
else if(arrInputSignUp.every(d => d.value !== "") && checkResult > -5 ){
document.getElementById('loadingData').classList.toggle('invisible');
sweetAlert("","ID No. taken. Enter a different one!!", "warning");
event.preventDefault();
}
else if(arrInputSignUp.every(d => d.value !== "") && checkResult > -6 ){
document.getElementById('loadingData').classList.toggle('invisible');
sweetAlert("","Phone taken. Enter a different one!!", "warning");
event.preventDefault();
}
else if(arrInputSignUp.every(d => d.value !== "") && checkResult > -7 ){
document.getElementById('loadingData').classList.toggle('invisible');
sweetAlert("","Email taken. Enter a different one!!", "warning");
event.preventDefault();
}
else (arrInputSignUp.every(d => d.value !== "") && checkResult == -1, -5, -6, -7 )
{
google.script.run
.withSuccessHandler( () => {
document.getElementById('loadingData').classList.toggle('invisible');
sweetAlert("","Data Saved!!", "success");
// login();
window.open(<?= scriptURL ?>, '_top');
} )
.saveSignUp(objInput);
};
})
.checkNewLabel(inputNewKeyword.value);
};
Related
Problem: The first conditional statement else if was performed, But the next else if statement was not performed. It should performed if both username and password are empty. What did I missed?
$(document).ready(function(){
$("#submit").click(function(e){
var username = $("#username").val();
var password = $("#password").val();
if(username == ""){
alert("account id required");
}else if(password == ""){
alert("password required!");
}else if((username == "" || username == null) && (password == "" || password == null)){
alert("All fields of information is required!");
}else{
$.ajax({
type: "POST",
url: "checklogin.php",
data: "username="+username+"&password="+password,
success:function(data){
if(data == "success"){
window.location.href="main.php";
}else if(data == "unsuccess"){
alert("Invalid account id/password");
$("#username").val('');
$("#password").val('');
}
}
});
}
return false;
});
});
If, else if, else if, else.. In this hierarchy. Any one and only one block will execute.. If your intention is to execute the second else if .. Then make it if...
So your code should be like below.
if((username == "" || username == null) && (password == "" || password == null)){
alert("All fields of information is required!");
}
else{
if(username == ""){
alert("account id required");
}else if(password == ""){
alert("password required!");
}
}
You have to put the condition containing both username and password before the other ones.
if it gets inside an if statement it wont try with the next else, so if it performs the first else if, it won't check the next else if.
you need to do something like this
if(!username && !password){
alert("All fields of information is required!");
}else if(!username){
alert("account id required");
}else if(!password){
alert("password required!");
}else{
here the rest of your code
}
i am validating a form and then asking for the confiormation through javascript…
so on submit i have called two function name validate() & make_confirm()..
onsubmit="return(validate() && show_alert(this));"
by this i am partially able to call both function but confirmation part is not working fine i have to redirect it to another page while pressing OK but its not going please help me out
validate & make_sure() function is as like:
function validate() {
if(document.getElementById('cname').value == '')
{
alert('Please enter your name');
document.getElementById('cname').focus();
return false;
}
else if(document.getElementById('address').value == '')
{
alert('Please enter your address');
document.getElementById('address').focus();
return false;
}
else if(document.getElementById('city').value == '')
{
alert('Please choose your city');
document.getElementById('city').focus();
return false;
}
else if(document.getElementById('state').value == '')
{
alert('Please enter your state');
document.getElementById('state').focus();
return false;
}
function make_sure() {
if(confirm("Do you really want to do this?"))
document.forms[0].submit();
else
return false;
}
Use one function for validate and confirm and set action of form to redirect form current page to another page.
function validateAndConfirm() {
if( isEmpty(id('cname').value) ) {
alert('Please enter your name');
id('cname').focus();
return false;
}
else if( isEmpty(id('address').value) ) {
alert('Please enter your address');
id('address').focus();
return false;
}
else if( isEmpty(id('city').value) ) {
alert('Please choose your city');
document.getElementById('city').focus();
return false;
}
else if( isEmpty(id('state').value) ) {
alert('Please enter your state');
id('state').focus();
return false;
} else {
if(confirm("Do you really want to do this?")) {
document.forms[0].submit();
}
else {
return false;
}
}
}
function isEmpty(val){
return (typeof val == 'undefined' || val === undefined || val == null || val.length <= 0) ? true : false;
}
// this function help to simplify you writing : document.getElementById to just id
function id(sid){
return document.getElementById(sid);
}
In my login script when the username and password are both correct it must go to the next page (main/menu page) of the intel XDK.
My problem is how or what code can I use to call the next page whenever the username and password is correct (login successful)?
function validateForm() {
var formUsername = document.forms.login.username.value;
var formPassword = document.forms.login.password.value;
var MINLENGTH = 5;
// Validate username and password
if (formUsername === null || formUsername === "") {
alert("Username must be filled out");
}
else if (formPassword === null || formPassword === "") {
alert("Password must be filled out");
}
else if (formUsername.length < MINLENGTH || formPassword.length < MINLENGTH) {
alert("The minimum length of username and password at least " + MINLENGTH);
}
else if(formUsername == 'admin' && formPassword == 'admin'){
alert('welcome');
//this is where should i put the code to go at the next page of the XDK API.
return;
}
alert("Login failed!!!");
}
got it guys..
it will be.. like this ..
else if(formUsername == 'admin' && formPassword == 'admin'){
alert('welcome');
activated_page("#menu");
return;
}
I'm trying to get a formula scripted properly, can some one help me please. I'm using Birt 3.7.1. thanks. This is for a Maximo report
if(row["special"] == 'W' && row["metername"] == null){
false} *** I need this coded --don't hide --> must have a task ***
else{
true}
if(row["special"] == 'W' && row["metername"] != null){
false} *** I need this coded --don't hide --> must have task --> view ***
else if(row["special"] == 'W' true
else true}
I'm not entirely sure what you're asking here, but if you're looking to just get your code here formatted validly, here you go:
if (row["special"] == 'W' && row["metername"] == null) {
return false;
} else {
return true;
}
if (row["special"] == 'W' && row["metername"] != null) {
return false;
} else if (row["special"] == 'W') {
return true;
} else {
return true;
}
I'm not sure its the most practical code, but there you go.
I have multple objects. Before submitting a form, I want to check to be sure all of the object values are "ok".
$("#sub_button").click(function() {
if (myObj.login_id && myObj.email == "ok") {
alert("This would submit the form. login ID obj is: "+myObj.login_id+" and email email obj is: "+myObj.email);
} else {
alert("the form is bad!");
}
});
I've tried it with two == and three ===, it still isn't working. The myObj.login_id or myObj.email could equal ANYTHING and it still pops up the "This would submit the form" alert, and shows in the alert box that the value(s) is NOT "ok".
What am I doing wrong?
This isn't comparing what you think it's comparing. When you read this aloud, it sounds right: "if login ID and email are equal to OK", but that's not how the && works. You want:
if (myObj.login_id == "ok" && myObj.email == "ok") {
....
}
The code you did write actually says "if login ID is anything at all, and if email is "ok" ".
$("#sub_button").click(function() {
if (myObj.login_id == "ok" && myObj.email == "ok") {
alert("This would submit the form. login ID obj is: "+myObj.login_id+" and email email obj is: "+myObj.email);
} else {
alert("the form is bad!");
}
});
You should do:
("#sub_button").click(function() {
if (myObj.login_id === "ok" && myObj.email === "ok") {
alert("This would submit the form. login ID obj is: "+myObj.login_id+" and email email obj is: "+myObj.email);
else {
alert("the form is bad!");
In your code you just check that myObj.login_id resolves to true (which happens everytime myObj.login_id value is not in this list)
false
null
undefined
The empty string ''
The number 0
The number NaN (yep, 'Not a Number' is a number, it is a special number)
Try this
$("#sub_button").click(function() {
if (myObj.login_id == "ok" && myObj.email == "ok") {
alert("This would submit the form. login ID obj is: "+myObj.login_id+" and email email obj is: "+myObj.email);
} else {
alert("the form is bad!");
(myObj.login_id && myObj.email == "ok") will evaluate to true if myObj.email == "ok" and myObj.login_id has a non-zero value. You're not evaluating the contents of login_id to anything, so it'll be handled as a boolean and converted to such according to the contents.
You should change it to (myObj.login_id == "ok" && myObj.email == "ok").