+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 13

Thread: Javascript Regex Pattern for decimal number(18,5)

  1. #1
    Barn Enthusiast guddu is on a distinguished road guddu's Avatar
    Join Date
    Jul 2008
    Location
    Oxford UK
    Posts
    471
    Rep Power
    4

    Javascript Regex Pattern for decimal number(18,5)

    hi
    i want to allow user only to input data in this format only
    Number(18,5).
    so far i just written for number only .how to write for (18,5). meant allow one decumal point and maximum 18 digits before decimal point and can be maixum 5 digits after decimal point.
    Code:
    var pattern = /^[0-9]+$/;
    
    Hope it make some sense.
    Last edited by richyrich; January 27th, 2009 at 12:17 PM.
    Love is physical attraction and mental destruction

  2. #2
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Try a pattern something like this:-
    Code:
    ^[0-9]{1,18}+(\.[0-9]{1,5})?$
    
    Hope that helps.

  3. #3
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Your example shows a comma (,) not a decimal point (.) ...

    Which do you need?
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  4. #4
    Barn Enthusiast guddu is on a distinguished road guddu's Avatar
    Join Date
    Jul 2008
    Location
    Oxford UK
    Posts
    471
    Rep Power
    4

    offcourse i need decimal.below is function i m calling on onkeypress event.
    Code:
    function fnClientNumericOnly()
          {
            var pattern = /^[0-9]{1,18}+(\.[0-9]{1,5})?$/;
            
    
            if(!pattern.test(String.fromCharCode(event.keyCode)))
        	{
        	    event.keyCode = 0;
        	}
    
          }
    
    getting object expected error.for this pattern var pattern = /^[0-9]+$/;
    i m not getting any error.
    Love is physical attraction and mental destruction

  5. #5
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Maybe something to do with the { and } I haven't used regex patterns in javascript before, so I'm unsure what you use instead....

  6. #6
    The Barnfather jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead has much to be proud of jmurrayhead's Avatar
    Join Date
    Mar 2008
    Location
    Reston, VA
    Posts
    4,547
    Blog Entries
    9
    Real Name
    Jason
    Rep Power
    22

    Here's a start:

    Code:
    /^[-+]?\d{1,18}(\.\d{1,5})?$/
    
    You'll have to fine-tune it if you have more requirements...
    jmurrayhead
    If you agree, give me rep.
    If you like it here...throw us a few bones to help support us.


  7. #7
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Try this:-
    Code:
    function test(value){
    var patt1;
      if(value.indexOf(".")==-1){
         patt1=new RegExp("^[0-9]{1,18}$");
      }else{
         patt1=new RegExp("^[0-9]{1,18}(\.[0-9]{1,5})?$");
      }
      if(patt1.test(value)){
        alert('Successful');
      }else{
        alert('No Match');
      }
    }
    
    I found it was the + that was causing the problem. I also found that if you didn't include a decimal point, it allowed you to add mor ethan 18 numbers. Not sure why that happened, but I tried the above and it seemed to work.

    You might want to make a few changes to fit in with your code. I just had it firing on a button click which passed the value of a textbox to the function.

    Hope that helps.

  8. #8
    Barn Enthusiast guddu is on a distinguished road guddu's Avatar
    Join Date
    Jul 2008
    Location
    Oxford UK
    Posts
    471
    Rep Power
    4

    Guys Thanks for a reply.i chekced code of both but problem is its not allowing decimal point.
    Love is physical attraction and mental destruction

  9. #9
    Administrator richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich is a splendid one to behold richyrich's Avatar
    Join Date
    Mar 2008
    Location
    Somewhere only we know...
    Posts
    3,207
    Blog Entries
    14
    Real Name
    Rich
    Rep Power
    14

    Hmm...Strange...Mine worked when I tested it...I couldn't get J's to work, but I checked mine against various values.
    Allowed
    Code:
    1
    123456789
    123456789123456789
    123456789.0
    123456.12345
    123456789.12345
    123456789123456789.12345
    
    Not Allowed
    Code:
    123456.123456
    123456789.
    1234567891234567891.12345
    123456789123456789.123456
    
    Have you tried the code provided exactly as is?

  10. #10
    Barn Enthusiast guddu is on a distinguished road guddu's Avatar
    Join Date
    Jul 2008
    Location
    Oxford UK
    Posts
    471
    Rep Power
    4

    no i just use this
    ("^[0-9]{1,18}(\.[0-9]{1,5})?$"); in my pattern actually i m testing this on my onkeypress event.
    Love is physical attraction and mental destruction

+ Reply to Thread
Page 1 of 2 1 2 LastLast

Similar Threads

  1. Replies: 9
    Last Post: January 12th, 2009, 06:42 PM
  2. add increment number in date
    By guddu in forum JavaScript Programming
    Replies: 2
    Last Post: August 13th, 2008, 10:47 AM
  3. Page Number Manipulation
    By AOG123 in forum Microsoft Access
    Replies: 6
    Last Post: March 20th, 2008, 02:38 PM

Bookmarks

Posting Permissions

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

SEO by vBSEO