Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
--I am using 
<pre lang="xml"><ajaxToolkit:FilteredTextBoxExtender ID="filtertext" runat="server"
       FilterType="Numbers, Custom" ValidChars="." TargetControlID="txtid">
   </ajaxToolkit:FilteredTextBoxExtender>



--But i want only single dot in the textbox. Currently user can enter multiple dot.

Like i want

23.45
.45

if entered

48. (then it should make it as 48.00)

Kindly help
Posted

 
Share this answer
 
Comments
anurag19289 11-Jan-14 4:04am    
Thanks..this works
Karthik_Mahalingam 11-Jan-14 4:27am    
welcome anurag :)
try this:-
C#
document.getElementById('Textbox1').onkeypress = function (e) {
      if (e.keyCode === 46 && this.value.split('.').length === 2) {
        return false;
    }
}



try demo on:-http://jsfiddle.net/ygKHE/[^]
 
Share this answer
 
Comments
anurag19289 11-Jan-14 4:04am    
Thanks thats a good idea to split and check.
try with the help of contains function. like that
C#
TextBox tt = new TextBox();
       if (tt.ToString().Contains('.'))
       {
           //code here
       }

i hope this is help full for you. for any query hit to reply.
 
Share this answer
 
Comments
TrushnaK 10-Jan-14 6:50am    
he wants to allow at least single time.
You can try this regex.
^(?=\D*\d)\d*(\.\d+)?$
This may help.
 
Share this answer
 
Comments
TrushnaK 10-Jan-14 6:52am    
this regular expression can not add "." at single time.
he wants to add the "." at single time only.

But i want only single dot in the textbox. Currently user can enter multiple dot.
you can make use of below javascript.

JavaScript
function isNumberDecimalKey(evt, ctlName, decimalPlaces) {
           var key;

           var isShift;

           if (window.event) {

               key = window.event.keyCode;

               isShift = window.event.shiftKey ? true : false;

           } else {

               key = evt.which;

               isShift = evt.shiftKey ? true : false;

           }

           if (isShift) {

               switch (key) {

                   case 16:

                   case 37:

                       return false;

                       break;

                   default:

                       return false;

                       break;

               }

           }
           //debugger;
           var charCode = (evt.which) ? evt.which : event.keyCode;

           var txtValue = document.getElementById(ctlName.id).value

           //does not include current keypress
           if (charCode == 8 || charCode == 9 || charCode == 46 || charCode == 37 || charCode == 39)    // Backspace or tab or delete
               return true;


           if (charCode == 190 || charCode == 110) //decimal   Not allowing decimals
           {
               if (txtValue.indexOf(".") != -1)
                   return false;
               else
                   return true;
           }
           else if ((charCode > 57 || (charCode > 31 && charCode < 48)) && !(charCode >= 96 && charCode <= 105)) {     //48-0, 96-numpad 0 Omitting both 0(zeroes)
               return false;
           }
           else {
               if (decimalPlaces != null && txtValue.indexOf(".") != -1) {
                   var txtSplit = txtValue.split(".");
                   var caretPosition = doGetCaretPosition(ctlName);
                   if (txtSplit[1].length == decimalPlaces && caretPosition > txtSplit[0].length) {
                       // if input is over the max decimal place, detect whether current selection existing.
                       var selectionText = "";
                       if (window.getSelection) {
                           selectionText = window.getSelection();
                       }
                       else if (document.selection) {
                           selectionText = document.selection.createRange().text;
                       }
                       return selectionText.length != 0;
                   }
               }
               return true;
           }
       }

       function doGetCaretPosition(ctrl) {
           var CaretPos = 0;
           // IE Support
           if (document.selection) {
               ctrl.focus();
               var Sel = document.selection.createRange();
               Sel.moveStart('character', -ctrl.value.length);
               CaretPos = Sel.text.length;
           }
           // Firefox support
           else if (ctrl.selectionStart || ctrl.selectionStart == '0')
               CaretPos = ctrl.selectionStart;


           return (CaretPos);
       }



call it onkeydown event of textbox
return isNumberDecimalKey(event, this, 2);

where 2 is the number of decimal places allowed
 
Share this answer
 
v2
<pre lang="xml"><script type="text/javascript">
      function validate() {
          var num = document.getElementById("txtid").value;
          if (isNaN(num) == true) {

              alert("Please enter numeric value!");
              document.getElementById("txtid").value = "";

          }
      }
  </script>


<asp:textbox id="txtid" cssclass="textBox_for6tds" runat="server" onkeyup="javascript:validate();" xmlns:asp="#unknown">

--or it can be done in onblur()
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900