2

I have a jquery function which receives a parameter from it callers. Calling split() on the parameter throws error. Here is the function

function formatNairaCurrency(value) {
var formatedWithoutNaira;
var formattedAmount
//check if value is in kobo format
var splittedValue = value.split(".");//Throws error
if (splittedValue.length === 2) {
    formatedWithoutNaira = isNaN(splittedValue[0]) ? "" : splittedValue[0].toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    formattedAmount = "₦" + formatedWithoutNaira + splittedValue[1];
} else {
    formatedWithoutNaira = isNaN(value) ? "" : value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    formattedAmount = "₦" + formatedWithoutNaira + ".00";
}

return formattedAmount;}

The call var splittedValue = value.split("."); throws the error value.split is not a function

What am I missing?

I am calling this in a .cshtml file. This works in another function even on the same .js file. The difference is that the value was not a parameter but a value from a text box.

Your help is greatly appreciated.

2
  • 2
    value is a type which does not have the split() method. You haven't shown where you call formatNairaCurrency or what it's value is, so we can't really help. From the context of a currency, I would assume the value is a float, so try value.toString().split('.') Commented Mar 12, 2016 at 9:33
  • updated my answer to include some tools that can help to solve your problem. Commented Mar 12, 2016 at 9:40

1 Answer 1

1

If i understand your intention correctly you are trying to use split for string. Your error could be caused by the fact that value is not string. You need to debug or throw to console 'value'.

Edit: For example if

value is null, or value is undefinded this would most definitely cause your error. Testing for those conditions:

(value === null)
(typeof value === 'undefined')

If your value is number - that would cause error too. You need to cast number to string first. You can do it by

var valueAsString = value.toString();
valueAsString.split('.');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.