By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,481 Members | 1,211 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,481 IT Pros & Developers. It's quick & easy.

RegExp

P: n/a
Hi,

I need to create a dynamically pattern match
for validate a number input, first without
decimals and then with 2 or more decimals.

Thanks in advance,

Enzo
Aug 30 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Enzo wrote on 30 aug 2005 in comp.lang.javascript:
I need to create a dynamically pattern match
for validate a number input, first without
decimals and then with 2 or more decimals.


What do you mean by "dynamically"?

What did you try yourself?
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Aug 30 '05 #2

P: n/a
Hi Evertjan,

I tried this pattern:

/^-?\d+(\.\d{1,3})?$/

for 1 to 3 decimals, but I need to replace
the static values '1' & '3' with variables.

Thanks again,

Enzo
Aug 30 '05 #3

P: n/a
Enzo wrote on 30 aug 2005 in comp.lang.javascript:
Hi Evertjan,

I tried this pattern:

/^-?\d+(\.\d{1,3})?$/

for 1 to 3 decimals, but I need to replace
the static values '1' & '3' with variables.


myTestValue = 1.1234

a = 1
b = 3
myRegex = "^-?\\d+\\.\\d{"+a+","+b+"}?$"
re = new RegExp(myRegex,"");

alert( re.test(myTestValue) )
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Aug 30 '05 #4

P: n/a
*** Enzo wrote/escribió (Tue, 30 Aug 2005 11:17:10 GMT):
I need to create a dynamically pattern match
for validate a number input, first without
decimals and then with 2 or more decimals.


You can take this a start point:

var my_filter=new Array('foo', 'bar');
eval('var re_filter=/^(' + my_filter.join('|') + ')$/i;');

--
-- Álvaro G. Vicario - Burgos, Spain
-- http://bits.demogracia.com - Mi sitio sobre programación web
-- Don't e-mail me your questions, post them to the group
--
Aug 30 '05 #5

P: n/a
Alvaro G Vicario wrote on 30 aug 2005 in comp.lang.javascript:
*** Enzo wrote/escribió (Tue, 30 Aug 2005 11:17:10 GMT):
I need to create a dynamically pattern match
for validate a number input, first without
decimals and then with 2 or more decimals.


You can take this a start point:

var my_filter=new Array('foo', 'bar');
eval('var re_filter=/^(' + my_filter.join('|') + ')$/i;');


Never use eval(),
it is evil and not necessary.

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Aug 30 '05 #6

P: n/a
Gracias Alvaro,

pero como dice Evertjan, eval is 'evil' ;-)

Saludos,

Enzo
Aug 30 '05 #7

P: n/a
Works great! Thanks Evertjan!

Enzo
Aug 30 '05 #8

P: n/a
Enzo wrote on 30 aug 2005 in comp.lang.javascript:
pero como dice Evertjan, eval is 'evil' ;-)


Because it is true and has been explained so many times in this NG.

Please see the archive.
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Aug 30 '05 #9

P: n/a
JRS: In article <1Y***********************@telenews.teleline.es> , dated
Tue, 30 Aug 2005 12:05:17, seen in news:comp.lang.javascript, Enzo
<yu**@yuyu.com> posted :
I tried this pattern:

/^-?\d+(\.\d{1,3})?$/

for 1 to 3 decimals, but I need to replace
the static values '1' & '3' with variables.


Unless this is school work, you do not *need* to replace the values,
since you can also use (\.\d+) and test the length of the match.

<URL:http://www.merlyn.demon.co.uk/js-valid.htm>

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 31 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.