[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

I need a function in Javascript that can valid my value

Posted on 2011-10-20
7
Medium Priority
?
163 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:hidrau
7 Comments
 
LVL 11

Expert Comment

by:MacAnthony
ID: 37002975
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

0
 
LVL 41

Expert Comment

by:HonorGod
ID: 37003134
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

0
 
LVL 11

Accepted Solution

by:
MacAnthony earned 668 total points
ID: 37003424
At first, I didn't understand what valueMinor and valueuser were. After rereading it a couple of times, I see now. And yes, you were right mine didn't work where it had an error.

My version rewritten:
function checkValue(valueMinor,ValueUser) {
    try {
        var val = parseInt(ValueUser);
        var diff = (val % valueMinor);
        return ( diff > 0) ? val+valueMinor-diff : val;
    } catch(e) {}
}

Open in new window


I think mine is a tad easier to read. OP's choice.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 9

Assisted Solution

by:keyu
keyu earned 664 total points
ID: 37005629
you just need one input variable for your functionm

function checkValue(ValueUser)
 {
         var acval = parseInt(ValueUser);
         var remder = 20-(acval % 20);

if (remder==0)
{
return true;
}
else
{
         var valrtrn=remder + acval ;
alert("valid upper value"+valrtrn);
         return false;
}
   }
0
 
LVL 11

Expert Comment

by:MacAnthony
ID: 37006292
I think they wants to be able to supply the min value so that it can be something other than 20.
0
 
LVL 9

Assisted Solution

by:experts1
experts1 earned 668 total points
ID: 37007784
Code below works and should be self explanatory:

<html>
<head>
<title>Valid Value</title>
<SCRIPT language=JavaScript type=text/javascript>
 function checkValue(valueMinor,ValueUser) {
   // Prevent Divide by 0 error and ensure numeric values only
    if (valueMinor == 0 ||  isNaN(valueMinor)) {    
     document.getElementById('val_minor').value = 1;
     valueMinor = 1;
     }
    if (isNaN(ValueUser)) {
     document.getElementById('val_input').value = valueMinor;
     ValueUser = valueMinor;
     }
   var z_minor = parseInt(valueMinor);
   var z_user = parseInt(ValueUser);
   var z_div = z_user / z_minor;
   var z_diff = z_user % z_minor;
   var z_int = z_div - z_diff;
    if (z_user > z_minor) {
     document.getElementById('val_input').value = (z_user - z_diff) + z_minor;
      } else {
     document.getElementById('val_input').value = z_minor;
     }
 }
</script>
</head>
<body>


MINOR VALUE<input id='val_minor' type='text'>
<br>
<br>
INPUT VALUE<input id='val_input' type='text' onchange="checkValue(document.getElementById('val_minor').value,this.value)">

</body>
</html>
0
 
LVL 1

Author Closing Comment

by:hidrau
ID: 37101405
thanks very much
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

865 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question