File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes JavaScript+Regular Expression Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "JavaScript+Regular Expression Problem" Watch "JavaScript+Regular Expression Problem" New topic
Author

JavaScript+Regular Expression Problem

aravind kanganar
Ranch Hand

Joined: Nov 08, 2009
Posts: 36
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
Marshal

Joined: Mar 22, 2005
Posts: 39534
    
  27
Try constructing the regexp via "new RegExp("^[0-9]{1,4}(\.[0-9]{1,3})?$") instead of using the /.../ notation.


Ping & DNS - updated with new look and Ping home screen widget
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Wouldn't the question mark allow one *or more* occurrences of that last grouping?

Moved to JavaScript.
aravind kanganar
Ranch Hand

Joined: Nov 08, 2009
Posts: 36
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

Joined: Sep 29, 2008
Posts: 12617

(I was thinking of the "+" character--sorry about that!)
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Nov 08, 2009
Posts: 36
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

Joined: Nov 08, 2001
Posts: 15376
    
    6
Are you sure you are not cached with an older version?



Works fine for me.

Eric
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JavaScript+Regular Expression Problem
 
Similar Threads
A regex problem
Validation Using Regular Expression in Javascript
Easy Regular Expression question
validating double value using regular expression
Regular expression to check for specific special characters (any repitition) and 0 to 9 numbers