开发者

text box percentage validation in javascript

How can we do validation for percentage numbers in textbox . I need to validate these type of data

E开发者_如何学编程x: 12-3, 33.44a, 44. , a3.56, 123

thanks in advance sri


''''Add textbox'''''

<asp:TextBox ID="PERCENTAGE" runat="server"
onkeypress="return ispercentage(this, event, true, false);"
MaxLength="18" size="17"></asp:TextBox>

'''''Copy below function as it is and paste in tag..'''''''

 <script type="text/javascript">
 function ispercentage(obj, e, allowDecimal, allowNegative) 
 {
   var key;
   var isCtrl = false;
   var keychar;
   var reg;
   if (window.event) 
   {
     key = e.keyCode;
     isCtrl = window.event.ctrlKey
   }
   else if (e.which)
   {
     key = e.which;
     isCtrl = e.ctrlKey;
   }
   if (isNaN(key)) return true;
   keychar = String.fromCharCode(key);
   // check for backspace or delete, or if Ctrl was pressed
   if (key == 8 || isCtrl)
   {
     return true;
   }
   ctemp = obj.value;
   var index = ctemp.indexOf(".");
   var length = ctemp.length;
   ctemp = ctemp.substring(index, length);
   if (index < 0 && length > 1 && keychar != '.' && keychar != '0')
   {
     obj.focus();
     return false;
   }
   if (ctemp.length > 2)
   {
     obj.focus();
     return false;
   }
   if (keychar == '0' && length >= 2 && keychar != '.' && ctemp != '10') {
     obj.focus();
     return false;
   }
   reg = /\d/;
   var isFirstN = allowNegative ? keychar == '-' && obj.value.indexOf('-') == -1 : false;
   var isFirstD = allowDecimal ? keychar == '.' && obj.value.indexOf('.') == -1 : false;
   return isFirstN || isFirstD || reg.test(keychar);
}
</script>


You can further optimize this expression. Currently its working for all given patterns.

^\d*[aA]?[\-.]?\d*[aA]?[\-.]?\d*$


If you're talking about checking that a given text is a valid percentage, you can do one of a few things.

  • validate it with a regex like ^[0-9]+\.?[0-9]*$ then just convert that to a floating point value and check it's between 0 and 100 (that particular regex requires a zero before the decimal for values less than one but you can adapt it to handle otherwise).
  • convert it to a float using a method that raises an exception on invalid data (rather than just stopping at the first bad character.
  • use a convoluted regex which checks for valid entries without having to convert to a float.
  • just run through the text character by character counting numerics (a), decimal points (b) and non-numerics (c). Provided a is at least one, b is at most one, and c is zero, then convert to a float.

I have no idea whether your environment support any of those options since you haven't actually specified what it is :-)

However, my preference is to go for option 1, 2, 4 and 3 (in that order) since I'm not a big fan of convoluted regexes. I tend to think that they do more harm than good when thet become to complex to understand in less than three seconds.


Finally i tried a simple validation and works good :-(

function validate(){ var str = document.getElementById('percentage').value; if(isNaN(str)) { //alert("value out of range or too much decimal"); } else if(str > 100) { //alert("value exceeded"); } else if(str < 0){ //alert("value not valid"); } }

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜