Checking input fields(text) value when field is loosing focus

Discussion in 'Javascript' started by JohnnyW, Mar 17, 2006.

  1. JohnnyW

    JohnnyW Guest

    Hello,

    I have I simple question that is not clear for me.
    So, I have a input field (text field) in my html page.
    User should give input as a string like following 12 345 678,0

    If user gives input in format 12345678,0 and leves the input field
    how can I format the value 12345678,0 into format 12 345 678,0?

    Are there any Javascript that can handle this with Regular Expressions?

    Thanks!
    JohnnyW, Mar 17, 2006
    #1
    1. Advertising

  2. JohnnyW wrote:

    > [...] I have a input field (text field) in my html page.
    > User should give input as a string like following 12 345 678,0
    >
    > If user gives input in format 12345678,0 and leves the input field
    > how can I format the value 12345678,0 into format 12 345 678,0?
    >
    > Are there any Javascript that can handle this with Regular Expressions?


    Do something like the following when the blur event of that control occurs:

    var s = "12345678,0";
    var rx = /(\d)((\d{3})+,\d+)$/;
    while (rx.test(s = s.replace(rx, "$1 $2"))) ;

    // just for debugging
    window.alert(s);

    I do not think it can be done without the loop, but I would be glad to be
    proven wrong.


    PointedEars
    Thomas 'PointedEars' Lahn, Mar 18, 2006
    #2
    1. Advertising

  3. JohnnyW

    Randy Webb Guest

    Thomas 'PointedEars' Lahn said the following on 3/17/2006 11:50 PM:
    > JohnnyW wrote:
    >
    >> [...] I have a input field (text field) in my html page.
    >> User should give input as a string like following 12 345 678,0
    >>
    >> If user gives input in format 12345678,0 and leves the input field
    >> how can I format the value 12345678,0 into format 12 345 678,0?
    >>
    >> Are there any Javascript that can handle this with Regular Expressions?

    >
    > Do something like the following when the blur event of that control occurs:


    No, you do something like that when the onchange event is fired, not
    onblur as onblur causes the code to be needlessly executed every time
    the field is blurred.

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
    Randy Webb, Mar 18, 2006
    #3
  4. Randy Webb wrote:

    > Thomas 'PointedEars' Lahn said the following on 3/17/2006 11:50 PM:
    >> JohnnyW wrote:
    >>> If user gives input in format 12345678,0 and leves the input field
    >>> how can I format the value 12345678,0 into format 12 345 678,0?
    >>>
    >>> Are there any Javascript that can handle this with Regular Expressions?

    >>
    >> Do something like the following when the blur event of that control
    >> occurs:

    >
    > No, you do something like that when the onchange event is fired, not
    > onblur as onblur causes the code to be needlessly executed every time
    > the field is blurred.


    True, my bad.


    PointedEars
    Thomas 'PointedEars' Lahn, Mar 18, 2006
    #4
  5. JohnnyW

    BootNic Guest

    > "JohnnyW" <> wrote:
    > news:dvf2v9$ttt$....
    >
    > Hello,
    >
    > I have I simple question that is not clear for me.
    > So, I have a input field (text field) in my html page.
    > User should give input as a string like following 12 345 678,0
    >
    > If user gives input in format 12345678,0 and leves the input field
    > how can I format the value 12345678,0 into format 12 345 678,0?
    >
    > Are there any Javascript that can handle this with Regular
    > Expressions?
    >
    > Thanks!

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <script type="text/javascript">
    function filter(x){
    var s = x.value;
    s=s.replace(/\D+/g,'');
    if(s.length==9){
    s=s.replace(/(\d{2})(\d{3})(\d{3})(\d)/,'$1 $2 $3,$4');
    x.value=s;
    }
    else{
    alert('incorrect input');
    x.select();
    setTimeout(function(){x.focus();},0);
    }
    }
    </script>
    <title></title>
    </head>
    <body>
    <input type="text" maxlength="12" onchange="filter(this)">
    </body>
    </html>

    --
    BootNic Saturday, March 18, 2006 9:55 AM

    When I was young, I was put in a school for retarded kids for two
    years before they realized I actually had a hearing loss...and they
    called ME slow!
    *Kathy Buckley*
    BootNic, Mar 18, 2006
    #5
  6. JohnnyW

    JohnnyW Guest

    Thank you for all of you guys!


    "BootNic" <> wrote in message
    news:4cVSf.20700$...
    > "JohnnyW" <> wrote:
    > news:dvf2v9$ttt$....
    >
    > Hello,
    >
    > I have I simple question that is not clear for me.
    > So, I have a input field (text field) in my html page.
    > User should give input as a string like following 12 345 678,0
    >
    > If user gives input in format 12345678,0 and leves the input field
    > how can I format the value 12345678,0 into format 12 345 678,0?
    >
    > Are there any Javascript that can handle this with Regular
    > Expressions?
    >
    > Thanks!

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <script type="text/javascript">
    function filter(x){
    var s = x.value;
    s=s.replace(/\D+/g,'');
    if(s.length==9){
    s=s.replace(/(\d{2})(\d{3})(\d{3})(\d)/,'$1 $2 $3,$4');
    x.value=s;
    }
    else{
    alert('incorrect input');
    x.select();
    setTimeout(function(){x.focus();},0);
    }
    }
    </script>
    <title></title>
    </head>
    <body>
    <input type="text" maxlength="12" onchange="filter(this)">
    </body>
    </html>

    --
    BootNic Saturday, March 18, 2006 9:55 AM

    When I was young, I was put in a school for retarded kids for two
    years before they realized I actually had a hearing loss...and they
    called ME slow!
    *Kathy Buckley*
    JohnnyW, Mar 18, 2006
    #6
  7. JRS: In article <dvf2v9$ttt$>, dated Fri, 17 Mar
    2006 21:34:05 remote, seen in news:comp.lang.javascript, JohnnyW
    <> posted :

    >I have I simple question that is not clear for me.
    >So, I have a input field (text field) in my html page.
    >User should give input as a string like following 12 345 678,0
    >
    >If user gives input in format 12345678,0 and leves the input field
    >how can I format the value 12345678,0 into format 12 345 678,0?
    >
    >Are there any Javascript that can handle this with Regular Expressions?


    <URL:http://www.merlyn.demon.co.uk/js-index.htm#RecTS> combined with
    ..replace(/\./g, ",") .


    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
    <URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
    Dr John Stockton, Mar 18, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roger
    Replies:
    3
    Views:
    311
  2. Replies:
    0
    Views:
    148
  3. DaveForgotHisEngineering

    Newbie problem checking input fields

    DaveForgotHisEngineering, Aug 26, 2008, in forum: Javascript
    Replies:
    3
    Views:
    82
    Dr J R Stockton
    Aug 27, 2008
  4. Andrew Falanga
    Replies:
    3
    Views:
    88
    Dr J R Stockton
    Apr 18, 2009
  5. jr
    Replies:
    3
    Views:
    409
Loading...

Share This Page