Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Sep 2002
    Location
    South East UK. 35 miles east of London, in sight of the River Thames.
    Posts
    300
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Reg Expr to allow numbers only

    Hi All,

    I have been reading regular expression tutorials all afternoon and I cannot get the hang of these things. I'm missing something fundamental in the syntax. However, some months ago, one of the good guys on the board gave me the following reg ex:

    onblur="this.value = this.value.replace(/ /g,'').replace( /^(.{2,4})(.{3})(.*)$/, '$1 $2' )"

    With this, you can automatically add the space to a UK post code, so if someone enters "SS75TE", the onblur feature changes this post code to "SS7 5TE".

    So far, so good.

    I now need to ensure that a currency field is converted into numbers only, so, if someone enters:

    £ 5,567.76

    The onblur will change it to:

    5567

    Can someone please give me the reg ex to do this (and a brief idiots explanation would be very useful ) )

    Thanks

    Gary

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    You could do a replace like this:
    Code:
    onblur="this.value=this.value.replace(/\D/g,'');"
    /D means any character but a digit, and you replace those characters with an empty string.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Regular Coder
    Join Date
    Sep 2002
    Location
    South East UK. 35 miles east of London, in sight of the River Thames.
    Posts
    300
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Hi Liorean,

    Thanks, that works fine and gets rid of £ and , and .

    I can actually follow what is happening. Now, if I enter:

    £5,575.98

    I now get

    557598

    How do I get rid of the 2 digits (98) after the decimal point before removing the decimal point itself?

    Regards

    Gary

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Do this first:
    Code:
    replace(/\.\d*$/, '')
    It removes a fullstop, followed by zero or more digits, followed by the end of the string.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    Regular Coder
    Join Date
    Sep 2002
    Location
    South East UK. 35 miles east of London, in sight of the River Thames.
    Posts
    300
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Hi Liorean,

    Yes! That did it, the following reg ex works AND I can follow the logic.

    Many thanks

    Gary


    onblur="this.value=this.value.replace(/\.\d*$/, '').replace(/\D/g,'');"


  •  

    Posting Permissions

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