0

I have a input field in my html which basically sends the amount to an observable and then redirects to different pages depending on the amount, but I want to make sure that even if a person who doesnt write amount and types in "string values" should still be able to get redirected, but I am not sure how to accomplish that.

HTML CODE

<input id="amount"  type="text" data-bind="value : amount" />
<button class="btn button2" type="submit"  data-bind=" valueUpdate:'afterkeydown' , click: $root.borrow_first_pageview" ></button>

I do not want to write the type= number in the HTML since I want to know how its checked in the JS.

here is the rest code using knockout.js

self.borrow_first_pageview = function () {
        if(self.amount()){
            window.location.href = BASEURL + "index.php/moneyexchange/borrow_first_page/" + self.amount(); 
        }else if(typeof self.amount() == 'string'){
            window.location.href = BASEURL + "index.php/moneyexchange/borrow_first_page/" + 2500;
        }else {
             window.location.href = BASEURL + "index.php/moneyexchange/borrow_first_page/" + 2500;
        }

    };

Is there a way to check if self.amount() is a String and then redirect the user. Need help.

Masnad Nihit
  • 1,986
  • 2
  • 21
  • 40
  • 1
    Possible duplicate of [Check if a variable is a string](http://stackoverflow.com/questions/4059147/check-if-a-variable-is-a-string). (Though perhaps slightly masked by other details in the question, amongst one that `self.amount` is *not* executed to get its value in the `else if` condition. But a dupe most likely nonetheles...). Alternatively, it may be a dupe of [Check that variable is a number](http://stackoverflow.com/q/1352577/419956) – Jeroen Mar 10 '16 at 10:59
  • So.. what you actually want to check is if the amount is a decimal/integer. You can use parseInt/parseFloat for that – mbx-mbx Mar 10 '16 at 11:00
  • @Magrangs no I want to check if someone puts a string instead of amount, since the function crashes if someone puts a string so I need to check it and then redirect them. – Masnad Nihit Mar 10 '16 at 11:01
  • @MasnadNihit yeah so if it parses as a decimal/int you know its not a string – mbx-mbx Mar 10 '16 at 11:01
  • @Jeroen its not that much similar, since I tried typeOf but I am not sure how to make it work. – Masnad Nihit Mar 10 '16 at 11:02
  • yea, so is there a way to check if self.amount() == parseFloat?? @Magrangs – Masnad Nihit Mar 10 '16 at 11:03

1 Answers1

3

So we can reverse the problem and see if the amount is a number or not and then act accordingly:

var value = self.amount();
if((+value == value) && !isNaN(+value)){
    //Yey we have a valid number.
}

This is probably one of the few valid uses of a loose equality operator I have found (it guards against null and "" being passed in). It uses the unary plus operator plus a little bit of magic and it's a nice short way of checking if a value is a number or not.

You can put this into a function if you like, 'IsNumber' for example:

function isNumber(value){
    //loose equality operator used to guard against nulls, undefined and empty string
    return ((+value == value) && !isNaN(+value));
}
mbx-mbx
  • 1,765
  • 11
  • 23
  • This works! thanks alot, I am not sure what +value and isNan(+value) means but it does work. – Masnad Nihit Mar 10 '16 at 11:11
  • 1
    Ha, yeah, it's a little bit cryptic but it's concise. the + is called the unary plus which does a coercion on the value, try it in the console. isNaN checks the if the value is Not a Number. The loose equality operator == doesn't do a strict comparison (using types. e.g. +"2" == "2" is true but +"2blah" == "2" is false) so that combined with isNaN check gives a way to check if a variety of numbers are actually numbers. – mbx-mbx Mar 10 '16 at 11:16