Link to home
Start Free TrialLog in
Avatar of hidrau
hidrauFlag for Brazil

asked on

I need a function in Javascript that can valid my value

Hello Guys

I have a value and this value is the minor value, for example:

20

The user will enter with its value, for example: 55

The function must check if the value is a multiple of 20, then, the system can only accept

20, 40, 60, 80, 90...

If the user enter with 55, the system must indicate a valid upper value, for example : 60

if the user enter 71 the system must appoint the upper valid value of 80.

Function checkValue(valueMinor, ValueUser){
}

thanks
Avatar of MacAnthony
MacAnthony
Flag of United States of America image

Try this:

function checkValue(valueMinor,ValueUser) {
    try {
        var val = parseInt(valueMinor);
        var offset = 20-(val % 20);
        return val+offset;
    } catch(e) {}
}

Open in new window

Avatar of HonorGod
I don't think that will do it.
I think it needs to be a little more complicated, perhaps something like this
<html>
<body>

<script type='text/javascript'>
  function checkValue( valueMinor, ValueUser ) {
    var result = 0
    try {
        var user = parseInt( ValueUser )
        var low  = user - valueMinor
        var rem  = low % 20
        result = ( user - rem ) + ( ( rem > 0 ) ? 20 : 0 )
    } catch(e) {}
    alert( 'result: ' + result )
    return result
  }
</script>

<input type=text onchange='checkValue(20,this.value)'>
</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of MacAnthony
MacAnthony
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think they wants to be able to supply the min value so that it can be something other than 20.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hidrau

ASKER

thanks very much