-2

Please can anyone respond to this... its very urgent..

the function should validate the below 1.23,0.05.0.3,0.00,89.45632 and should not accept 0.,00123,045,.,89.,563245.,12..3,12...,hjhj,gh.23,h. there can be atleast one decimal point

Thanks in Advance Radhika

Raghu
  • 85
  • 3
  • 3
  • 9
  • 3
    maybe you should better describe what is a valid format, using just a few example is not always sufficient to behave correctly in the border cases – Eineki May 12 '11 at 11:36
  • I assume `0.05.0.3` is a typo as a valid format? – Alex K. May 12 '11 at 11:38
  • 1
    Surprisingly hard to find. Perhaps http://regexlib.com/REDetails.aspx?regexp_id=2699 – mplungjan May 12 '11 at 11:40
  • 4
    "it is very urgent" is considered [very rude](http://www.catb.org/~esr/faqs/smart-questions.html#urgent). Also: if you want good answers, format your question to be readable: your examples are hardly readable. – Joachim Sauer May 12 '11 at 11:43
  • 1
    I agree they are not understandable. Something like this works better: http://stackoverflow.com/questions/5379231/displaying-currency-in-indian-numbering-format – mplungjan May 12 '11 at 11:51

1 Answers1

0

Here is a function that returns true if it is given a decimal number or a string representing a decimal number, and false otherwise.

I hope that is what you wanted.


function validate(s){
    return /\d+(.\d+)/.test(s)&&!/^0\d+/.test(s);
}
If you want the function also accepts natural numbers like "2", "0", or "10" then use this instead:

function validate(s){
    return /\d+(\.\d+)?/.test(s)&&!/^0\d+/.test(s);
}
Luc125
  • 5,752
  • 34
  • 35