Q:

javascript regular expression for percentage between 0 and 100

Possible Duplicate:
Javascript percentage validation 

I want to allow 0.00 to 100.00 only. 
function ValidateText(i)
    {

        if(i.value.length>0)
        {
         i.value = i.value.replace(/[^\d.d]+/g, '');

        }
    }
<asp:textbox id="txtrate" runat="server" Width="200px"  onkeyup= "javascript:ValidateText(this)"></asp:textbox>

It allows 0-9.0-9.  Help me please.  Thanks 

A:

Now this is some popular question! 
This should do:
function validate(s) {
  return s.match(/^(100(\.0{1,2})?|[1-9]?\d(\.\d{1,2})?)$/) ! = null;
}

var test = [
  '3.0',
  '5',
  '99.99',
  '100',
  '100.00',
  '100.01',
  '101',
  '0.3',
  '.5',
  '0.567',
];

for (i=0; i<test.length; ++i) {
  WScript. StdOut. WriteLine(test[i] + ' => ' + validate(test[i]));
}

Outputs:
3.0 => true
5 => true
99.99 => true
100 => true
100.00 => true
100.01 => false
101 => false
0.3 => true
.5 => false
0.567 => false

Edit: the regexp can be shortened a bit without changing its meaning, credits to 6502
/^(100(\.00?)?|[1-9]?\d(\.\d\d?)?)$/

A:

This expression should allow only what you are asking for
/^(\d\d?(\.\d\d?)?|100(\.00?)?)$/

Meaning is
^            start of string
(            start of sub-expression ("or" between two possibilities)
\d\d?         one or two digits
(\.\d\d?)?    optionally followed with a dot and one or two digits
|            or
100          the string "100"
(\.00?)?      optionally followed by a dot and one or two zeros
)            end of sub-expression
$            end of string

