!isValid not accepting any value from input - javascript

Right now the !isValid is not accepting any values in the input for current balance. Is there another way to verify the value in the input? JSFiddle
The function in question:
function isValid(entry, a, b)
{
if (isNaN(entry.value) || (entry.value==null) || (entry.value=="") || (entry.value < a) || (entry.value > b)) {
alert("Invalid entry. Your min payment should be between " + a + " and " + b + ".")
return false
}
return true
}
// send entries to validation function
// exit if not valid
if (!isValid(currentBalance, 0, 10000)) {
return false
} else if (!isValid(interest, 0, 30)) {
return false
} else {
return true;
}
if (!isValid(mnth_pay, currentBalance*.02, currentBalance)) {
return false
} else {
console.log("do nothing");
}

I've just adjusted your Fiddle.
As I first added console.msg to get the values handled by the function isValid(entry, a, b), I noticed that entry.value is undefined, but entry matches the current input.
Adjusting to
function isValid(entry, a, b)
{
if (isNaN(entry) || (entry==null) || (entry=="") || (entry < a)
|| (entry.value > b))
{... }
}
seems to fix the issue.

Related

Greater than or not equal to

My code here
if (parent4 && parent5 && parent6 && (_state[tree][parent4]) + (_state[tree][parent5]) + (_state[tree][parent6]) !== 8) {
return false;
} else {
return true;
}
It works, and has some of the elements I want, namely that if the sum of the three _states !== 8, it returns false.
I'll try to explain the logic I'm trying to achieve as simply as I can:
if par4 + par5 + par6 !>= 8
return false
if par4 + par5 !>= 8
return false
if par4 OR par5 OR par6 !>= 8
return false
else
return true
I abbreviated the code for simplicity and ease of understanding.
Swapping around return false and return true and changing it to >= doesn't work, because for all elements, even ones I don't mention here, it needs to go to return true by default.
How about this?
if (parent4 && parent5 && parent6) {
if ((_state[tree][parent4] + _state[tree][parent4] + _state[tree][parent4]) !== 8) {
return false;
} else {
return true;
}
}
Or in a one-liner:
if (parent4 && parent5 && parent6 && (_state[tree][parent4] + _state[tree][parent4] + _state[tree][parent4]) !== 8) {
return false;
} else {
return true;
}

loop won't go through first if statement

My code will only go through to the first if statement where it checks the value of key for headline1 etc... The first if statement works properly but it won't work with any of the following if statements when the first one isn't true. I've switched the second statement to the first where it checks for 'desc1' and then it works for that one only.
The purpose of this function is to check each key of an object and return the key when its value is over a certain length so I can add a class and show user some warning. This is in Vue JS so ads is in data and characterCheck is in computed property.
ads: [
{
headline1: '_keyword_',
headline2: 'Online',
headline3: 'Free',
desc1: 'Buy online _keyword_',
desc2: ' Vast collection of _keyword_',
finalurl: 'www.books.com',
path1: '',
path2: '',
boolean: true
}
]
characterCheck () {
for(var x = 0; x < this.ads.length; x++){
if(this.ads[x]['boolean'] == true) {
for(var key in this.ads[x]){
var length = this.ads[x][key].replace(/_keyword_/g, this.activeKeyword).length
if( key === 'headline1' || key === 'headline2' || key === 'headline3'){
if(length > 30){
return key
}
} else if( key == 'desc1' || key == 'desc2'){
if(length > 90){
return key
}
} else if( key == 'path1' || key == 'path2'){
if(length > 15){
return key
}
} else {
return false
}
}
}
}
}
When your first nested if condition fails, the code goes to next subsequent else-if. For some particular value, all the if and else-if block fails and code lands on final else block which contains a return statement.
If your code reaches even once there, the entire function execution immediately stops and false value is returned.
Since, you wish to wait as long as you have not looped through all the values, remove the else part and add a simple return statement to the end of the for loop like this:
function characterCheck () {
for(var x = 0; x < this.ads.length; x++) {
if(this.ads[x]['boolean'] == true) {
for(var key in this.ads[x]) {
var length = this.ads[x][key].replace(/_keyword_/g, this.activeKeyword).length
if( key === 'headline1' || key === 'headline2' || key === 'headline3') {
if(length > 30) {
return key
}
}
else if( key == 'desc1' || key == 'desc2') {
if(length > 90) {
return key
}
} else if( key == 'path1' || key == 'path2') {
if(length > 15) {
return key
}
}
}
}
}
return false
}

Javascript gives undefined instead of the ex.message

The code below works, only when I type a letter in the prompt(), I get undefined message instead of the WrongValue ex.message which is in the catch(ex). I have tried a lot of varations but I still dont know what is wrong. How do I do this correctly?
var myList = ["Oranges", "Apples", "Pineapples", "Bananas"];
var getFruit = function(index) {
if (index > myList.length || index < 0) {
throw new RangeError("The number you gave doesn't exist in the list, the number must be 0 <= # <= " + myList.length);
} else {
return myList[index];
}
if (isNaN(index)) {
throw new WrongValue("Give a number please");
} else {
return myList[index];
}
}
try {
getFruit(prompt("Which fruit are you looking for"));
} catch (ex) {
if (ex instanceof RangeError) {
console.log(ex.message);
}
if (ex instanceof WrongValue) {
console.log(ex.message);
}
}
check isNaN FIRST ... change getFruit to the following
var getFruit = function(index) {
if(isNaN(index)) {
throw new WrongValue("Give a number please");
else if(index > myList.length || index < 0) {
throw new RangeError("The number you gave doesn't exist in the list, the number must be 0 <= # <= " + myList.length);
} else {
return myList[index];
}
}
As written now always one of your condition that will be checked since they've return and throw the both prevent the code from continue to the next instruction, so try to conbine the both condition in one and check the isNaN() first, it like:
var getFruit = function(index) {
if (isNaN(index)) {
throw new WrongValue("Give a number please");
} else if (index > myList.length || index < 0) {
throw new RangeError("The number you gave doesn't exist in the list, the number must be 0 <= # <= " + myList.length);
} else {
return myList[index];
}
}
Hope this helps.

Validating numeric values using JavaScript

I have the following code. It works fine for blank fields, but it doesn't catch the other numeric exceptions. What am I doing wrong?
function validateForm() {
var a = document.forms["Form"]["percentage"].value;
var b = document.forms["Form"]["minutes"].value;
if (a == null || b == null || a == "" || b == "") {
alert("Please Fill All Required Field");
return false;
} else if (isNan(a) == true || isNan(b) == true) {
alert("Please enter valid numeric values");
return false;
} else if (parseInt(a) > 100) {
alert("Percentage can't exceed 100");
return false;
} else if (parseInt(b) < 0 || parseInt(a) < 0) {
alert("Values can't be negative");
return false;
}
}
Change this line:
else if((isNan(a)==true) ||(isNan(b)==true)){
to this:
else if (isNaN(a) || isNaN(b)) {
as the function is named #isNaN(). Using == true in conditionals is quite redundant, so I removed them.
I have also made a fiddle for you. It contains the fixed code, and it is working well.

Where in the code to validate a phone number, in JavaScript orRazor?

This is my first webpage in which I prompt the user for a phone number to add to a Do Not Call List database. Everything is working so far but I need to add the following, which I can do following the advice in this answer
stripping the input from all characters except digits
validating that the resulting string is 10 digits long
Then, when telling the user that the number was added to the list, I want to present it in the (999) 999-9999 format.
Where should I add all that code? Iside the #{ } block? In JavaScript? Razor?
Check phone number
function IsNumber(s) {
var i, currentCharacter;
for (i = 0; i < s.length; i++) {
// Check that current character is number.
currentCharacter = s.charAt(i);
if (((currentCharacter < "0") || (currentCharacter > "9"))) {
return false;
}
}
// All characters are numbers.
return true;
}
function TestInternationalPhone(strPhone) {
var bracket = 3,
openBracket,
phoneNumberOnly,
phoneNumberDelimiters = "()- ",
validWorldPhoneChars = phoneNumberDelimiters + "+",
minDigitsInIPhoneNumber = 10;
strPhone = SOS.StringHelper.Trim(strPhone);
if (strPhone.length === 0) {
return false;
}
if (strPhone.indexOf("+") > 1) {
return false;
}
if (strPhone.indexOf("-") != -1) {
bracket = bracket + 1;
}
if (strPhone.indexOf("(") != -1 && strPhone.indexOf("(") > bracket) {
return false;
}
openBracket = strPhone.indexOf("(");
if (strPhone.indexOf("(") != -1 && strPhone.charAt(openBracket + 2) != ")") {
return false;
}
if (strPhone.indexOf("(") == -1 && strPhone.indexOf(")") != -1) {
return false;
}
phoneNumberOnly = SOS.StringHelper.StripCharsInBag(strPhone, validWorldPhoneChars);
return (IsNumber(phoneNumberOnly) && phoneNumberOnly.length >= minDigitsInIPhoneNumber);
}

Categories