I have a formula I use to determine how opaque some validation text should be based upon the length of a user's input compared to the maximum lenth allowed. I want to modify it so that the "ramping up" of the opacity percentage only starts when they are at 80% of max, and then scales up proportionally from there.

Here is my current function:

OpacityPercentage = CharactersEntered / MaxCharacters

Therefore, if I have MaxCharacters of 50, then the opacity is as follows:

  • 30 chars = 60%
  • 40 chars = 80%
  • 41 chars = 82%
  • 45 chars = 90%
  • 50 chars = 100%

What I want is for the opacity to be 0% until I get to 80% of max, then scale up from there. So I would want the table to look as follows:

  • 30 chars = 0%
  • 40 chars = 0%
  • 41 chars = 10%
  • 45 chars = 50%
  • 50 chars = 100%

I thought this would be simple, but I can't seem to figure out what I need to change in my existing formula. Any advise is appreciated!

  • $\begingroup$ Could you just take the maximum of $0$ and $( \text {CharactersEntered}-40)\cdot 10$? $\endgroup$ – David Mitra Feb 17 '12 at 23:03
  • $\begingroup$ That's it! I knew it was simple, but I couldn't wrap my head around it. If you would, can you post your comment as an answer and I'll accept it. Thanks again! $\endgroup$ – bmccleary Feb 17 '12 at 23:17

Could you just take the maximum of $0$ and $(\text{CharactersEntered}−40)\cdot10$ (percent)?

Added from the comment:

If you wanted to start the percentages off at $x$ and have them increase linearly to $100$ at $50$ characters, then you are adding $(100-x)/10$ percent for each character over $40$. The number of characters over 40 is $$ I=\text{max}\{(0, \text{CharactersEntered}−40\}. $$ Given $I$, the percentage would be, for $I>0$, $x$ plus $I\cdot {100-x\over 10}$ percent. We need a function that tells us if $I>0$ for the following, so let $G=0$ if $I\le 0$ and $G=1$ if $I\ge 1$. Then the formula (probably not the most efficient) is $$ G\cdot x + \text{max}\{(0, \text{CharactersEntered}−40\}\cdot {100-x\over 10}. $$

| cite | improve this answer | |
  • $\begingroup$ Thanks! One last question... what if I wanted the threshold to be another value like 60% or 75% (instead of the 80%). I thought I just needed to change the "x 10" at the end of your equation, but it's not working. What value would I use to multiply by for these other percentages? $\endgroup$ – bmccleary Feb 17 '12 at 23:54
  • $\begingroup$ @bmccleary Sorry, my last comment was a bit off. I expanded the answer to address your question. $\endgroup$ – David Mitra Feb 18 '12 at 0:39
  • $\begingroup$ Got it! Thanks again. $\endgroup$ – bmccleary Feb 18 '12 at 0:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.