Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JavaScript+Regular Expression Problem

 
aravind kanganar
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • 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
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • 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.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • 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
  • 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
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(I was thinking of the "+" character--sorry about that!)
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • 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
  • 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
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure you are not cached with an older version?



Works fine for me.

Eric
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic