#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Need to restrict numeric entry to two decimal places


    Hello geniuses,

    I have a cfform with a cfinput of type=text. The cfinput accepts salary values. How can I restrict it to accept only two decimal places?

    I have tried mask, but it will only accpet two decimal places once the first part of the mask is satisfied. For instance, a mask of "99999.99" will work great for 12345.67 but will not accept 12.34. Similarly, a mask of "99.99" works for 12.34, but will not accept 12345.67.

    I am very on this, it seems like it should be simple but I cannot figure it out. I do not know JavaScript.

    Thank you in adavance,
    Jim
  2. #2
  3. No Profile Picture
    Moderator

    Join Date
    Jun 2002
    Location
    Raleigh, NC
    Posts
    5,286
    Rep Power
    968
    You'll need to use a regular expression to validate the format. Something like this (which you'll have to tweak depending on your exact needs):

    <cfinput required="Yes" validate="regex" pattern="^[0-9]{0,10}\.[0-9]{2,2}$" name="testcf">
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    5
    Rep Power
    0
    Kiteless,

    It is an honor to hear from you, thank you so much for your help.

    I added your code to my cfinput, but it is not working, so I'm wondering what I did wrong. Forgive the length, but my cfinput looks like: <cfinput type="Text" name="amount" message="Required Field: Please enter the Amount." required="Yes" size="#size#" maxlength="10" tabindex="76" class="body" style="width:250px" validate="regex" pattern="^[0-9]{0,10}\.[0-9]{2,2}$">

    I would like to try to understand what the pattern syntax means. Is it stating that it should allow up to 10 digits, then the decimal, and then a mandatory two digits after the decimal?

    Thank you,
    Jim
  6. #4
  7. No Profile Picture
    Moderator

    Join Date
    Jun 2002
    Location
    Raleigh, NC
    Posts
    5,286
    Rep Power
    968
    Yes, that's what the pattern means. Since the default validation event is onSubmit, you can also try setting validateAt to "onBlur", which should validate it as soon as the field loses focus.

    To be honest, the built-in CF masking and validation is meant to handle some common, basic scenarios. Most people don't actually use it. Building your own JavaScript or using one of the many well-established libraries available (jQuery, etc.) is much more common. But since you said you don't know JavaScript, this may not be an option for you.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    5
    Rep Power
    0
    Kiteless,

    Thank you for sticking with me, I'm sorry if my lack of knowledge is a huge hinderance to solving this problem or makes it frustrating for you to work with me.

    I set the ValidateAt to onblur but it is still not working. I have tried on IE, FF, and Chrome in case a browser setting was making a difference.

    I'm not sure what else to do or what can be done. Thank you again for your patience and help.

    Jim
  10. #6
  11. No Profile Picture
    Moderator

    Join Date
    Jun 2002
    Location
    Raleigh, NC
    Posts
    5,286
    Rep Power
    968
    Without being able to debug in the browser or inspect the generated source, I'm not sure.

    One way you can confirm is to try it with a field like this, which should only pass validation if the field has letters in it:

    <cfinput name="fieldName" type="text" size="20" maxlength="20" required="yes" validate="regex" pattern="^[A-Za-z]{1,20}$" message="Only characters are allowed." />

    Just to make sure you understand, the regex validation will throw a validation error if the regex doesn't match. This doesn't do the masking that you originally asked for. (That's only going to be possible with custom JavaScript).

    If that still doesn't work, and you're able to submit the form with anything other than letters in the input field, then something else has to be wrong with your setup.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    5
    Rep Power
    0
    Kiteless,

    Thank you again for your patience and help.

    I wanted to let you know that I have come up with a solution to my situation, but that I used JavaScript to do it instead of ColdFusion.

    I don't know JavaScript to write it, but I can follow along with some of it's simplest forms. I was given ideas by a few colleagues of mine and I pursued them, and with their help and the Internet's, I was able to come up with something that works.

    Here's what I did, please forgive the horrible cut-and-paste:

    function checknumber(){
    decimalLocation = document.PositionForm.amount.value.indexOf(".");
    var toBeSliced
    if (document.PositionForm.amount.value.indexOf(".") != -1){
    toBeSliced = document.PositionForm.amount.value.slice(0,decimalLocation+3);}
    else {
    toBeSliced = document.PositionForm.amount.value;
    }
    document.PositionForm.amount.value = toBeSliced;
    }

    I then call this function using onBlur so that it fires whenever the textbox loses focus, and it works!

    I had to use the IF statement because otherwise, if no decimal was used, the function would chop off all but the first two digits, so 123456 would turn into just 12.

    Thank you again for all of your help. Is there something I can do to award you points?
  14. #8
  15. No Profile Picture
    Moderator

    Join Date
    Jun 2002
    Location
    Raleigh, NC
    Posts
    5,286
    Rep Power
    968
    Glad you got it working. And yes, honestly, beyond the basic built-in options you're much better off doing it yourself with JS. Knowing JS is becoming pretty much a mandatory skill anyway.

    If you like, you can use the little scales in the top right of my post to "add to my reputation".

    Regards,

    Brian

    Comments on this post

    • wmhintz agrees : Kiteless is very smart and kind and deserves credit.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    5
    Rep Power
    0
    Brian,

    I wanted to let you know that I clicked on the scales, but when the window popped-up it wouldn't let me assign anything but 0, so I made sure to put a comment. Sorry I couldn't give you any points, probably because I'm a first-timer?

    Thank you again.

    Jim
  18. #10
  19. No Profile Picture
    Moderator

    Join Date
    Jun 2002
    Location
    Raleigh, NC
    Posts
    5,286
    Rep Power
    968
    Thanks but no worries. The reputation score is nice but it's not really important to me. :-)
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    9
    Rep Power
    0
    Originally Posted by kiteless
    Thanks but no worries. The reputation score is nice but it's not really important to me. :-)
    But it'd be nice it the feature worked tho right.

IMN logo majestic logo threadwatch logo seochat tools logo