How to use Javascript to allow only quarter increments such as .25, .5, .75, and .0

My HR Department wants to only allow quarter hour increments on an Acrobat form. I see it allows for custom Javascript validation. How can I use Javascript to allow only quarter increments on the end of the string such as .25, .5, .75, and .0
LVL 2
smetterdAsked:
Who is Participating?
 
BadotzConnect With a Mentor Commented:
Can you add

onclick="bump(this);"

to the form field declaration?

Change the function to:


function bump(obj) {
    if (obj.value) {
        
        var num = obj.value;
        
        if (isNaN(num) {
            return ""; // Not a number
        }
        else {
            return parseFloat(obj.value) += .25;
        }
    }
    else {
        return ""; // No VALUE attribute
    }
}

Open in new window

0
 
BadotzCommented:
This function will do what you need:


function bump(num) {
    return num += .25;
}

Open in new window

0
 
smetterdAuthor Commented:
Okay, but how would I enter is in to the acrobat field as a custom validation script where the form field is names "HOURSRow1" without the quotes?
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
smetterdAuthor Commented:
The only triggers I see are as follows

 Triggers
0
 
BadotzCommented:
When do you want the value increased?
0
 
ProculopsisCommented:

Try something like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_26847810.html</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript"> 

jQuery(document).ready(function() {

  $("#check").click( checkQuarter );
  
});

function checkQuarter() {
  var value = parseFloat( $("#HOURSRow1").val() );
  var isValid = ( parseInt( value * 4 ) == value * 4 );

  alert( isValid );
}

</script>
</head>
<body>

  <input id="HOURSRow1" />
  <input id="check" value="Check" type="button" />

</body>
</html>

Open in new window

0
 
BadotzCommented:
jQuery? Really? Hammer this thumbtack with a flaming meteorite the size of Texas?
0
 
ProculopsisCommented:

<!-- jQuery? Really? Hammer this thumbtack with a flaming meteorite the size of Texas? -->

<form>
  <input id="HOURSRow1" />
  <input id="check" value="Check" type="button" onclick="alert( ( parseInt( this.form['HOURSRow1'].value * 4 ) == this.form['HOURSRow1'].value * 4 ) && !isNaN( parseFloat( this.form['HOURSRow1'].value ) ) );" />
</form>
0
 
smetterdAuthor Commented:
Badotz, I would like the value to be checked when the user tabs out of that field in the PDF. Thx for the help.
0
 
BadotzCommented:
Then use the onblure event:

onblur="bump(this);"

Not sure exactly how to specify this in Acrobat-speak, though...
0
 
smetterdAuthor Commented:
Thank you Badotz!
0
 
BadotzCommented:
No worries - glad to help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.