Results 1 to 9 of 9

Thread: [RESOLVED] Put a comma on last 2 digits while typing in a textbox.

  1. #1
    King of sapila
    Join Date
    Oct 06
    Location
    Greece
    Posts
    3,546

    Resolved [RESOLVED] Put a comma on last 2 digits while typing in a textbox.

    Hi.I don't have a VS in front of me but i would be needing this one next week when i start working again,so i thought i would be prepared.What i want to do is put a comma "," right before the last 2 digits.So i am thinking of ajax masked edit extender but i don't have VS here so i can't do the testings.If it does not have the exact feature i was thinking of some custom javascript-jquery function .Finally a regex to clear out the wrong formats-characters again if masked extender does not work.
    My formatting should be like this: 1234567 to 12345,67 , 1 to 1,00 etc.I should be trying this myself but i appreciate a head up from anyone of you so i would have it ready to go next week when i get my hands to Visual Studio any help would be appreciated.If doable, i would prefer the mask for the masked extender.
    Thanks.
    Slow as hell.

  2. #2
    Frenzied Member brin351's Avatar
    Join Date
    Mar 07
    Location
    Land Down Under
    Posts
    1,257

    Re: Put a comma on last 2 digits while typing in a textbox.

    Id just use JS on the keyup event of the textbox, would only take a few lines so if you don't use the masked extender (I never have so no comment) I'd be happy to try my hand and post the JS.
    The problem with computers is their nature is pure logic. Just once I'd like my computer to do something deluded.

  3. #3
    King of sapila
    Join Date
    Oct 06
    Location
    Greece
    Posts
    3,546

    Re: Put a comma on last 2 digits while typing in a textbox.

    Hey.I have no problem with js or jquery, i have seen some samples and saved them in order for me to try them next week when i would be able to have a Visual Studio enabled computer.So,of course, feel free to post any script.Thanks.
    P.S. This is for textboxes that cover doctors general examination values on patience so i suppose it won't need a dot also(p.e. it would be 1000,00 and not 1.000,00) i am just saying that because i haven't been given that exact information and everyone here(including me) are on vacation and i cannot find anyone till the end of the month to clarify this info.So if you have any opinion or experience on this do also advice.But i believe no dot is needed.Thanks again.
    P.S.2 Unfortunately i would be going on another island so no free WIFI as off,err,8 hours from now,so i would probably be able to see this on Friday.So take your time.Going to bed...
    Edit.Sorry i forgot this.I would like, if possible the comma to appear at the first keyup so when someone writes 1 it would be ,01 when it presses 1 again would be ,11 then again would be 1,11 and again 11,11 etc.
    Last edited by sapator; Aug 12th, 2012 at 11:14 PM.
    Slow as hell.

  4. #4
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 04
    Location
    The Granite City
    Posts
    21,838

    Re: Put a comma on last 2 digits while typing in a textbox.

    Hey Sap,

    Here is an example of this being done when the focus is lost of the TextBox:

    http://scottonwriting.net/sowblog/ar...ormatting.aspx

    Which I am sure could be modified to act as you require.

    Hope that helps!

    Gary

  5. #5
    Frenzied Member brin351's Avatar
    Join Date
    Mar 07
    Location
    Land Down Under
    Posts
    1,257

    Re: Put a comma on last 2 digits while typing in a textbox.

    Quote Originally Posted by sapator View Post
    Edit.Sorry i forgot this.I would like, if possible the comma to appear at the first keyup so when someone writes 1 it would be ,01 when it presses 1 again would be ,11 then again would be 1,11 and again 11,11 etc.
    well that complicates matters but check this out

    Code:
        <script type="text/javascript">
            function formatNumber(txt){
            
                var input = txt.value
                var lgth = input.length
                var lastDigit = input.charAt(lgth-1)
                            
                if (isNaN(lastDigit)){
                    //char is not a number
                    txt.value = input.substr(0,lgth-1)
                    alert("Numbers Only")
                }else{
                    //format text
                                    
                    num = input.replace(",", "") //remove comma
                    if (num.substr(0,1) == 0){
                        num = num.substr(1,lgth-1) //remove 0 from start
    
                    }
                    
                    if (num.length == 0){
                        //enterd 0 as first char
                        txt.value = ""
                        //alert("Can't start with 0")
                    }else if (num.length == 1){
                        txt.value = ",0" + num
                    }else if (num.length == 2){
                        txt.value = "," + num
                    }else{
                        last2Digits = num.substr(lgth-3,lgth-1)
                        firstDigits = num.substr(0,lgth-3)
                        txt.value = firstDigits + "," + last2Digits
                    }
                                   
                }
                        
            }       
        </script>
    on the textbox add onkeyup="formatNumber(this)"
    The problem with computers is their nature is pure logic. Just once I'd like my computer to do something deluded.

  6. #6
    King of sapila
    Join Date
    Oct 06
    Location
    Greece
    Posts
    3,546

    Re: Put a comma on last 2 digits while typing in a textbox.

    Apparently being drunk and writing stuff is not very good advisor as i actually am leaving tomorrow for the other island
    So thank you i will try them next week back home.
    brin one observation.I saw on the script that zero cannot be the first char.This is not the case because someone may want a value to be p.x. 0,06 .
    Thanks again will report when i try them.
    Slow as hell.

  7. #7
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 04
    Location
    The Granite City
    Posts
    21,838

    Re: Put a comma on last 2 digits while typing in a textbox.

    Have a safe trip!

    Gary

  8. #8
    King of sapila
    Join Date
    Oct 06
    Location
    Greece
    Posts
    3,546

    Re: Put a comma on last 2 digits while typing in a textbox.

    Hey.So i just had a quick run and i am using masked extender with a filter.Thanks.
    Slow as hell.

  9. #9
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 04
    Location
    The Granite City
    Posts
    21,838

    Re: [RESOLVED] Put a comma on last 2 digits while typing in a textbox.

    Noice! Glad to hear that you got it working!

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •