• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JavaScript+Regular Expression Problem

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have to take a floating number as input from a jsp page.
The floating number has following constrains:
1. Before the dot there can be at the max 4 digits and atleast 1 digit
2. One can either enter or leave the dot
3. If one enters the dot after it he/she should also enter atleast 1, upto 3 digits.

So I wrote the following regular expression for it, which goes as an input to a javasript:

/^[0-9]{1,4}(\.[0-9]{1,3})?$/

But it doesn't seem working.
My page takes numbers like 11111,222222 ....
Can any one give me some idea on this please?

Thanks in advance,
Aravind
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try constructing the regexp via "new RegExp("^[0-9]{1,4}(\.[0-9]{1,3})?$") instead of using the /.../ notation.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wouldn't the question mark allow one *or more* occurrences of that last grouping?

Moved to JavaScript.
 
aravind kanganar
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

Thanks Ulf Dittmer:) I'm going try it.


I thought '?' allows either zero or one occurance of previous expression.I'm checking that also...

Thanks for your help:)

-Aravind
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(I was thinking of the "+" character--sorry about that!)
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your regular expression fails since you are not accounting for the beginning and ending of the string

12345 passes since 2345 is valid.

This is probably what you want to use:


Eric
 
aravind kanganar
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I tried but still in same old situation.

I was matchig the word boundary too...

Tried the following options:
1. new RegExp("^[0-9]{1,4}(\.[0-9]{1,3})?$")

2. /^[0-9]{1,4}(\.[0-9]{1,3})?$/

But when I enter numbers like 123456 (which are not supposed to be accepted ), they get accepted.

May be the original problem is somewhere else in the code... I need to take a deeper look.

Thanks for all of your help,

-Aravind
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure you are not cached with an older version?



Works fine for me.

Eric
 
Whatever you say buddy! And I believe this tiny ad too:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic