Restricting text entry in a textbox to numericals only
-
Hi I have a textbox on my webform. I want to restrict the data entered into the textbox to numericals only. When someon types into the textbox they must not be able to type any other character except numbers. Is it possible .. Please help with sample code Thanks zimcoder What Democracy?? Jesus Christ is King and if you do not like... well you can go to hell!
-
Hi I have a textbox on my webform. I want to restrict the data entered into the textbox to numericals only. When someon types into the textbox they must not be able to type any other character except numbers. Is it possible .. Please help with sample code Thanks zimcoder What Democracy?? Jesus Christ is King and if you do not like... well you can go to hell!
Add the following javascript function to your .aspx page. In the textbox control add onkeypress="return Textbox1KeyPress(event)" in .aspx file. function Textbox1KeyPress(event) { var Decimal = 46 var DeciPlaces = 4 var AllowNegative = false var myString = new String(event.srcElement.value); var pntPos = myString.indexOf(String.fromCharCode(Decimal)); var keyChar = window.event.keyCode; if ((keyChar < 48) || (keyChar > 57)) { if (keyChar == Decimal) { if ((pntPos != -1) || (DeciPlaces < 1)) { return false; } return true; } else if (((keyChar == 45) && (!AllowNegative || myString.length != 0)) || (keyChar != 45)) {return false;} } return true; } This is a multipurpose function and will check for decimal places and negative values. Set the maxlength attribute to limit the input values for the textbox. Cheers;) </x-turndown>
-
Add the following javascript function to your .aspx page. In the textbox control add onkeypress="return Textbox1KeyPress(event)" in .aspx file. function Textbox1KeyPress(event) { var Decimal = 46 var DeciPlaces = 4 var AllowNegative = false var myString = new String(event.srcElement.value); var pntPos = myString.indexOf(String.fromCharCode(Decimal)); var keyChar = window.event.keyCode; if ((keyChar < 48) || (keyChar > 57)) { if (keyChar == Decimal) { if ((pntPos != -1) || (DeciPlaces < 1)) { return false; } return true; } else if (((keyChar == 45) && (!AllowNegative || myString.length != 0)) || (keyChar != 45)) {return false;} } return true; } This is a multipurpose function and will check for decimal places and negative values. Set the maxlength attribute to limit the input values for the textbox. Cheers;) </x-turndown>
-
Hi I have a textbox on my webform. I want to restrict the data entered into the textbox to numericals only. When someon types into the textbox they must not be able to type any other character except numbers. Is it possible .. Please help with sample code Thanks zimcoder What Democracy?? Jesus Christ is King and if you do not like... well you can go to hell!
Public Const strNumeric_OnKeypress_WO_Comma_and_Period As String = "javascript:if (event.keyCode < 48 || event.keyCode > 57) event.returnValue = false;" Public Const strNumeric_OnKeypress_W_Comma_and_Period As String = "javascript:if ((event.keyCode < 48 && event.keyCode != 46 && event.keyCode != 44) || event.keyCode > 57) event.returnValue = false;" Public Const strNumeric_OnKeypress_W_Period As String = "javascript:if ((event.keyCode < 48 && event.keyCode != 46) || event.keyCode > 57) event.returnValue = false;" Add the three constant variable in code behind and use the attribute of textbox control like this. txtNoOfDeposit.Attributes("onkeypress") = strNumeric_OnKeypress_WO_Comma_and_Period
-
Public Const strNumeric_OnKeypress_WO_Comma_and_Period As String = "javascript:if (event.keyCode < 48 || event.keyCode > 57) event.returnValue = false;" Public Const strNumeric_OnKeypress_W_Comma_and_Period As String = "javascript:if ((event.keyCode < 48 && event.keyCode != 46 && event.keyCode != 44) || event.keyCode > 57) event.returnValue = false;" Public Const strNumeric_OnKeypress_W_Period As String = "javascript:if ((event.keyCode < 48 && event.keyCode != 46) || event.keyCode > 57) event.returnValue = false;" Add the three constant variable in code behind and use the attribute of textbox control like this. txtNoOfDeposit.Attributes("onkeypress") = strNumeric_OnKeypress_WO_Comma_and_Period
Hi there, This solution just prevent user stroke, but cannot prevent user drag & drop restricted text from another. Now I still have not found the solution for this Could u help me to solve this? thanks in advance :((:-O
-
Hi there, This solution just prevent user stroke, but cannot prevent user drag & drop restricted text from another. Now I still have not found the solution for this Could u help me to solve this? thanks in advance :((:-O
Hi, Sorry for my late reply. You can not prevent the user to Paste or drag and drop some text but you can add additional validation in "onblur" attributes like checking if the text is numeric or in a right format. ex. Public Const strNumeric_OnKeypress_WO_Comma_and_Period As String = _ "javascript:if (event.keyCode < 48 || event.keyCode > 57) event.returnValue = false;" Public Const strNumeric_OnKeypress_W_Comma_and_Period As String = _ "javascript:if ((event.keyCode < 48 && event.keyCode != 46 && event.keyCode != 44)" & _ "|| event.keyCode > 57) event.returnValue = false;" Public Const strNumeric_OnKeypress_W_Period As String = _ "javascript:if ((event.keyCode < 48 && event.keyCode != 46)" & _ "|| event.keyCode > 57) event.returnValue = false;" Public Const strNumeric_OnBlur = "javascript:if(isNaN(this.value)==true) " & _ {window.alert("invalid numeric input!");this.value=''}" '~~~ Add the 4 constant variable in code behind and use the attribute '~~~ of textbox control like this. '~~~ Validation on keypress txtNoOfDeposit.Attributes("onkeypress") = strNumeric_OnKeypress_WO_Comma_and_Period '~~~ Validation on OnBlur or Lost of Focus txtNoOfDeposit.Attributes("onblur") = strNumeric_OnBlur Happy Coding.... Mark
-
Hi there, This solution just prevent user stroke, but cannot prevent user drag & drop restricted text from another. Now I still have not found the solution for this Could u help me to solve this? thanks in advance :((:-O
Please Replace javascriptif by javascript::if
-
Hi, Sorry for my late reply. You can not prevent the user to Paste or drag and drop some text but you can add additional validation in "onblur" attributes like checking if the text is numeric or in a right format. ex. Public Const strNumeric_OnKeypress_WO_Comma_and_Period As String = _ "javascript:if (event.keyCode < 48 || event.keyCode > 57) event.returnValue = false;" Public Const strNumeric_OnKeypress_W_Comma_and_Period As String = _ "javascript:if ((event.keyCode < 48 && event.keyCode != 46 && event.keyCode != 44)" & _ "|| event.keyCode > 57) event.returnValue = false;" Public Const strNumeric_OnKeypress_W_Period As String = _ "javascript:if ((event.keyCode < 48 && event.keyCode != 46)" & _ "|| event.keyCode > 57) event.returnValue = false;" Public Const strNumeric_OnBlur = "javascript:if(isNaN(this.value)==true) " & _ {window.alert("invalid numeric input!");this.value=''}" '~~~ Add the 4 constant variable in code behind and use the attribute '~~~ of textbox control like this. '~~~ Validation on keypress txtNoOfDeposit.Attributes("onkeypress") = strNumeric_OnKeypress_WO_Comma_and_Period '~~~ Validation on OnBlur or Lost of Focus txtNoOfDeposit.Attributes("onblur") = strNumeric_OnBlur Happy Coding.... Mark
hi there, we can prevent user to Paste some text by checking the event OnPaste of textbox, but I dont known how to prevent user to Drag & drop :((:-O