Javascript regex for percentage between 0 and 100

Possible duplicate:
Javascript validation validation

I want to allow only from 0.00 to 100.00.

function ValidateText(i) { if(i.value.length>0) { i.value = i.value.replace(/[^\dd]+/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

+4
source share
7 answers

Now this is some kind of 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: regexp can be reduced a little without changing its value, 6502 loans

 /^(100(\.00?)?|[1-9]?\d(\.\d\d?)?)$/ 
+8
source

This expression should only allow what you ask

 /^(\d\d?(\.\d\d?)?|100(\.00?)?)$/ 

Value

 ^ 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 
+7
source

Try this one

 ^(?:\d{1,2}(?:\.\d{1,2})?|100(?:\.0?0)?)$ 

See here at Regexr

(?:) do not capture groups, this means that a match from this group is not stored in the variable.

\d{1,2} matches 1 or 2 digits

(?:\.\d{1,2})? This is optional . followed by 1 or two digits

or

100(?:\.0?0)?) Matches 100 and then 1 or 2 0

^ matches start of line

$ matches end of line

These two anchors are necessary, otherwise it will also correspond if there are things before or after the allowable number.

Update: I do not know, but if you want to disable leading zeros and numbers without two digits in the fractional part, try this:

 ^(?!0\d)(?:\d{1,2}(?:\.\d{2})|100\.00)$ 

I deleted the optional parts, so there should be a period and two digits after it.

(?!0\d) - this is a negative result, which guarantees that the number does not start with 0 and immediately the next digit.

+2
source

What about:

 var x = '76', // (i.value) testx = Number(x.match(/\d+/)[0]); console.log(testx>=0 && testx<=100); 

Applies to your function:

 function ValidateText(i) { var val = i.value; if (val.length>0) { var test = Number(val.match(/\d+/)[0]); return test >=0 && test <= 100; } return false; } 
0
source

Use this:

 function ValidateText(i) { if(i.value.length>0) { i.value = i.value.match(/[1?\d{1,2}\.\d\d]/)[0]; } } 

instead of replacing everything that isn't (0.00 - 100.00) (it seems to me you're trying to do), I match the valid strings and replace the original contents of the variable with just the matching string.

Keep in mind that this will work if you only have 1 match. If you have more, you need to fool the expression a bit and decide how to concatenate the array of results.

0
source

Use this regex: ^(?:100(?:\.0{1,2})?|[0-9]{1,2}(?:\.[0-9]{1,2})?)$

0
source

In fact, I do not see this as a problem with regex. I would probably write this, especially if you need informational error messages:

HTML:

 <input id="percentValue" type="text" size="20"> <input type="button" value="Check" onclick="checkPercent()"> 

JavaScript:

 function checkPercent() { var o = document.getElementById("percentValue"); var val = o.value; if (val.length == 0) { alert("Empty value"); return; } var index = val.search(/[^0-9\.]/); if (index != -1) { o.selectionStart = o.selectionEnd = index; alert("Invalid characters"); return; } if (val.match(/\./g).length > 1) { alert("Number must be of the form nn"); return; } var floatVal = parseFloat(val); if (floatVal < 0 || floatVal > 100) { alert("Value must be between 0.00 and 100.00"); return; } alert("Valid value of: " + floatVal.toFixed(2)); } 

jsfiddle here: http://jsfiddle.net/jfriend00/rDbAp/

0
source

All Articles