Javascript needs to prevent blanks from being entered - javascript

The following Javascript is attached to a field form (on change ) it is supposed to ensure that if the user clicks on a button then 'off site' will populate in activity_type. And if not then '95 Modifier' will appear. In addition this form sheet has a field I have checked 'required' yet what is happening is the user is able to enter blanks for activity type. Is there a way within this javascript to then not allow a blank to be entered?
if (getFormElement('activity_type_id').toUpperCase()=='EE641670-8BE3-49FD-8914-030740D9DE72'
&& getFormElement('actual_location').toUpperCase()!='5E74C25C-6363-46BE-B030-16216B364F5A')
{
setFormElement('is_off_site',true);
} else
{
setFormElement('is_off_site',false);
}
{
setFormElement('is_off_site',false);
}

For your requirement custom function might solve your issue. It might cover almost your all primary scenarios. I have tried my best to update an answer with the best possibilities.
Please review it.
function isEmpty(inputValue) {
if(typeof inputValue === 'string' && (inputValue === '0' || inputValue === 'false' || inputValue === '[]' || inputValue === '{}' || inputValue === '')){
return true;
}else if(Array.isArray(inputValue) === true){
return inputValue.length === 0 ? true : false;
}else if(Array.isArray(inputValue) === false && (typeof inputValue === 'object' && inputValue !== null) && typeof inputValue !== 'undefined' && typeof inputValue !== null){
return Object.keys(inputValue).length === 0 ? true : false;
}else if(typeof inputValue === 'undefined'){
return true;
}else if(inputValue === null){
return true;
}else if(typeof inputValue === 'number' && inputValue === 0){
return true;
}else if(inputValue === false){
return true;
}else if(inputValue.length > 0 && inputValue.trim().length === 0){
return true;
}
return false;
}
console.log("isEmpty(' '): ",isEmpty(' '));
console.log("isEmpty(''): ",isEmpty(''));
console.log("isEmpty([]): ",isEmpty([]));
console.log("isEmpty({}): ",isEmpty({}));
console.log("isEmpty(): ",isEmpty());
const nullValue = null;
console.log("isEmpty(null): ",isEmpty(nullValue));
console.log("isEmpty(0): ",isEmpty(0));
console.log("isEmpty(false): ",isEmpty(false));
console.log("isEmpty('0'): ",isEmpty('0'));
console.log("isEmpty('false'): ",isEmpty('false'));
console.log("isEmpty('[]'): ",isEmpty('[]'));
console.log("isEmpty('{}') ",isEmpty('{}'));
console.log("isEmpty(''): ",isEmpty(''));
console.log("isEmpty('0.0'): ",isEmpty(0.0));

Related

How to create correct multiple filter?

I am trying to show items of game and I want to filter my output results.
I have panel with multiple filters
And I don't know how to do it correct. Some filters are similar, maybe it should move to function.
I tried to do like this (for search by name and one filter), but I am not sure is it the best way or no?
filteredItems() {
if (this.activeFilter && this.activeFilter != "all") {
return this.items[this.activeFilter]
.filter(
item =>
item.name.toLowerCase().indexOf(this.search.toLowerCase()) !== -1
)
.filter(item => {
if (this.minLvl !== "" && this.maxLvl !== "") {
if (
item.itemLevel >= this.minLvl &&
item.itemLevel <= this.maxLvl
)
return true;
} else if (this.minLvl == "" && this.maxLvl !== "") {
if (item.itemLevel <= this.maxLvl) return true;
} else if (this.minLvl !== "" && this.maxLvl == "") {
if (item.itemLevel >= this.minLvl) return true;
} else {
return true;
}
});
}

How to fix isInteger for Internet Explorer [duplicate]

i have this error in internet explorer console ' Object doesn't support property or method 'isInteger' ' how can i resolve it ?
code:
function verificaNota(nota){
if (nota.length>0){
var arr = [];
if( nota.indexOf(".") != -1 ){
return ferificareArrayNote(nota.split('.'));
}else if( nota.indexOf(",") != -1 ){
ferificareArrayNote(nota.split(','));
}else if( nota.length<=2 && Number.isInteger(Number(nota)) && Number(nota)<=10 && Number(nota) > 0){
return true;
}else {
return false;
}
}
return true;
}
And yes, i pass it a number not char;
As stated by #Andreas, Number.isNumber is part of ES6 so not supported by IE11
You can add the following polyfill to you javasript
Number.isInteger = Number.isInteger || function(value) {
return typeof value === "number" &&
isFinite(value) &&
Math.floor(value) === value;
};
source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger

How to check null and undefined both values for the property?

I have two properties where i need to check null and undefined both for each, how can i use that in if else statements ?
main.js
var validateControlRating = function () {
if ( ($scope.controlProcessRatingDTO.controlPerformanceRatingKey === null ||
$scope.controlProcessRatingDTO.controlPerformanceRatingKey === undefined)
&&
($scope.controlProcessRatingDTO.controlDesignRatingKey === null ||
$scope.controlProcessRatingDTO.controlDesignRatingKey === undefined) ) {
$scope.caculatedRatingDiv = false;
} else {
$http.get('app/control/rest/calculateControlEffectiveness/' + $scope.controlProcessRatingDTO.controlDesignRatingKey + '/' + $scope.controlProcessRatingDTO.controlPerformanceRatingKey).success(function (data) {
$scope.calcaulatedRating = data;
}, function (error) {
$scope.statusClass ='status invalid userErrorInfo';
var errorMessage = error.data.errorMsg;
if (error.data.techErrorMsg) {
errorMessage = error.data.techErrorMsg;
}
$scope.statusInfo = errorMessage;
});
$scope.ratingValidationMsg = '';
$scope.ratingWinValidationClass = 'valid';
$scope.caculatedRatingDiv = true;
$scope.enableRatingSave = false;
}
};
It's a little tedious in javascript, you have to write each condition, and use parentheses etc
if ( ($scope.controlProcessRatingDTO.controlPerformanceRatingKey === null ||
$scope.controlProcessRatingDTO.controlPerformanceRatingKey === undefined)
&&
($scope.controlProcessRatingDTO.controlDesignRatingKey === null ||
$scope.controlProcessRatingDTO.controlDesignRatingKey === undefined) ) {...
or just
if ([null, undefined].indexOf( $scope.controlProcessRatingDTO.controlPerformanceRatingKey ) === -1
&&
[null, undefined].indexOf( $scope.controlProcessRatingDTO.controlDesignRatingKey ) === -1) {...
I think you need to to check this correclty, check for undefined then for null
and use && not || because your code will go to check null value for undefined variable and this surely will throw exception
code:
if( typeof myVar == 'undefined' ? false: myVar )
{ // go here defined and value not null
}
or
code:
if(typeof myVar != 'undefined' && myVar)
{ // go here defined and value not null
}
In your code check will go like
if ((typeof $scope.controlProcessRatingDTO.controlDesignRatingKey !== undefined||
typeof $scope.controlProcessRatingDTO.controlPerformanceRatingKey !== undefined) &&
($scope.controlProcessRatingDTO.controlDesignRatingKey !== null ||
$scope.controlProcessRatingDTO.controlPerformanceRatingKey !== null)) {
// do home work
}else { // do other home work }
You can use negate operator as well, but this would make work for "false" as well:
if (!$scope.controlProcessRatingDTO.controlPerformanceRatingKey && !$scope.controlProcessRatingDTO.controlDesignRatingKey) {
This is a bit shorter but if you want to treat False values separately, then use the Adeneo's answer above.
You could do this:
if ( some_variable == null ){
// some_variable is either null or undefined
}
taken from: How to check for an undefined or null variable in JavaScript?

javascript binary tree, check whether it is a mirror of itself

I learn javascript recently, I don't know why the code I wrote is wrong. Here is the quesion:Given a binary tree, check whether it is a mirror of itself.
var isSymmetric = function(root) {
if(root === null) return true;
function isSymmetric(leftNode, rightNode){
if(leftNode === null && rightNode === null) return true;
if(leftNode === null || rightNode === null) return false;
return (leftNode.val == rightNode.val) && isSymmetric(leftNode.left, rightNode.right) && isSymmetric(leftNode.right, rightNode.left);
}
isSymmetric(root.left, root.right);
};
when the input is 1, the result is "undefined". This algorithm is transformed from my Java code. Please kindly inform me where I get wrong.
var isSymmetric = function(root) {
if (root === null) return true;
function isSymmetric(leftNode, rightNode) {
if (leftNode === null && rightNode === null) return true;
if (leftNode === null || rightNode === null) return false;
return (leftNode.val == rightNode.val) && isSymmetric(leftNode.left, rightNode.right) && isSymmetric(leftNode.right, rightNode.left);
}
return isSymmetric(root.left, root.right);
};
you need to return the result of isSymmetric as shown above
personally, I wouldn't have the outer and inner functions have the same name, it looks confusing to my old eyes :p

My displayError() function is not working at all

I made a function that should display an error or remove an error.
Unfortunately, when I use the function in any way, for example like displayError(true, "test");, it's not working.
When I check my html code to see if anything changes, nothing is changed.
function displayError(display, string, xhr) {
$("div#error").fadeOut(300, function() {
if(arguments.length == 1 && typeof display === "boolean" && display == false) {
//do nothing
} else if(arguments.length == 2 && typeof display === "boolean" && display == true && typeof string == "string") {
$("div#error").html('<b style="color: #ce1919;">(!)</b> '+string).fadeIn(300);
} else if(arguments.length == 3 && typeof display === "boolean" && display == true && typeof string == "string" && typeof xhr === "object") {
$("div#error").html('<b style="color: #ce1919;">('+xhr.status+")</b> "+string).fadeIn(300);
}
});
}
Anybody who can identify the problems?
use typeof:
typeof display === "boolean"
typeof xhr === "object"
MDN typeof Reference
I found out about the console log method. So I tried to check if my function was fired at all and it was. But then I found that the arguments array was just empty, which meant that the parameters were not available in the callback. So I stored the arguments in a variable and then used that variable in my if statements in the callback.
Like so:
function displayError(display, string, xhr) {
var args = arguments;
$("div#error").fadeOut(300, function() {
if(args.length == 1 && typeof display === "boolean" && display == false) {
//do nothing
} else if(args.length == 2 && typeof display === "boolean" && display == true && typeof string == "string") {
$("div#error").html('<b style="color: #ce1919;">(!)</b> '+string).fadeIn(300);
} else if(args.length == 3 && typeof display === "boolean" && display == true && typeof string == "string" && typeof xhr === "object") {
$("div#error").html('<b style="color: #ce1919;">('+xhr.status+")</b> "+string).fadeIn(300);
}
});
}

Categories

Resources