-1

I need an expression that will match with any number that may or may not have a + or - before it and also may or may not have a decimal at any before or after any number. .432423 and 42343242. is valid but a single decimal is not, even though with the expression I am running a single decimal is passing my validity test. Please tell me what is wrong with this code rather than giving me an answer. Thank you!

var number = /^((\-|\+)?([\d*\.?\d+])|([\d+\.?\d*]))$/;
Ha_Riddler
  • 49
  • 1
  • 8
  • Are you trying to capture parts of the number or just to verify whether the number conforms to certain format? You are using a lot of _parenthesis_ which means group capture in regex for extracting parts of the number. – DJ. Dec 03 '15 at 19:58

2 Answers2

0

[\d*\.?\d+] matches any one character from that set, and so will match a single .. Removing both pairs of brackets makes it work correctly.

Emily
  • 543
  • 4
  • 12
0

look at this one

/^([-+]?[.]{1}\d+)|([-+]?\d+[.]{1}\d*)$/

with this one you have 2 match groups.. first give you numbers [+-].1234 and second one [+-]123.[123]

Czejeno
  • 56
  • 4
  • @JohnCarpenter yep, and I think that was intention of question owner, and if not, its only metter of changing {1} to ? after [.] if he wants integers – Czejeno Dec 03 '15 at 20:46
  • Hmmm, I think he wanted an optional decimal. Either way, good point. I'll delete my comment. – Frank Bryce Dec 03 '15 at 20:48