This may be a quick and easy answer. I am updating some code for an application that tracks goal data. Our client wants to add negative values for goals. It doesn't sound right, but they want to add it to dummy employees in order for balancing against company totals.
I have everything done, I am just trying to make things "idiot proof" in a manner of speaking and take care of all issues that could come up on their end. I have updated the "onKeyDown" javascript function that currently exists in this app to fix users entering in a -0 or just a - symbol. I want to add another layer to prevent them from putting the - symbol in the middle of a number, for instance 56-88 instead of -5688. This is the code I wrote to do this:
for (j=1; j< f.value.length; j++)
{
buf2 = f.value.substr(j,1);
if(buf2 == '-')
{
f.value = f.value.replace(f.value.substr(j,1),'');
}
}
I am starting the for loop at 1 to skip the first index of the string in case it is a - symbol, and going through each index checking for a - symbol and replacing it with nothing, basically deleting it. It works great, unless there is a - symbol at the beginning as well, then it removes the first - symbol and not the second.
For example, when testing the application I type 56-88 and it updates this field with 5688 as designed, however when I update it with -56-88 it should update it with -5688 but instead gives me 56-88. I am scratching my head at this as it looks like it should be working fine. Any help is appreciated!