Click here to Skip to main content
15,897,291 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
How to validate the textbox such that,
1) it should allow only numbers.
2) after decimal only 2 digits are allowed.
3) Maximum of 11 digits are allowed before decimal, the no of digits before decimal must be less than 11 digits.

any code is most welcome , JavaScript is most welcome........
thanks and regards in advance.........
Posted
Updated 29-Jul-11 6:03am
v3

Use AJAX Control Toolkit's MaskedEditExtender and MaskedEditValidator control. This is the best and Complete Solution for your Question.
 
Share this answer
 
You can use javascript for this. Example:
C#
//Javascript function:
function validate()
{
    var txttext=document.getElementById("TextBox1").value;
    var regex=/[0-9][.][0-9]{2}$/;
    alert(regex.test(txttext));
}


XML
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="javascript:return validate();"/>
 
Share this answer
 
v2
Comments
prabhu460 29-Jul-11 8:52am    
tx for u reply.....
but this is satisfying only first two conditions, it failed on 3rd condition.
less than 11 digits must be placed before decimal.
if u solve this , it will be great helpfull to me
tx in advance
C# Validating TextBox Control[^] should help you get started.
 
Share this answer
 
Comments
prabhu460 29-Jul-11 8:48am    
tx for u r fast response....
but , i want to validate the textbox on client side,
so can u pls me any javascript.......
You can use either an ASP.Net validation control or manually create the validation in JavaScript.

Using an ASP.Net Validation Control:
<asp:RegularExpressionValidator runat="server"
   id="DecimalTextBoxValidator"
   ControlToValidate="DecimalTextBoxID"
   ValidationExpression="/^[\d]{1,11}(.[\d]{1,2})?$/"
   ErrorMessage="Number format is invalid."
   EnableClientScript="True" />

The trick to get it to run on the client instead of the server is the EnableClientScript="True" attribute.

For pure JavaScript approaches:
var decimal_regex = /^[\d]{1,11}(.[\d]{1,2})?$/;
function validateDecimal(inputElement){
  return decimal_regex.test(inputElement.value);
}

// In the form validation:
if(validateDecimal(submittedForm['DecimalTextBoxID'])){
  //validation error
}

Or, my personal favorite, add a function to the String prototype and use that in your form validating script:
if(!String.prototype.isDecimal){
  // requires 1 at least digit and 1 decimal digit if separator present
  String.prototype.isDecimal = function(digits, decimals){
    if(digits == undefined){ digits = ""; }
    if(decimals == undefined){ decimals = ""; }
    var regex = new RegExp("^[\\d]{1,"+digits+"}(.[\\d]{1,"+decimals+"})?$");
    return regex.test(this);
  }
}

// In the form validation:
if(!submittedForm['DecimalTextBoxID'].value.isDecimal(11,2)){
  //validation error
}


I would avoid trying to filter input on the text box with javascript (that's a trick and half without a 3rd party library). I would also not do an onBlur/.focus() trap on the box as that tends to frustrate users more than anything else.
 
Share this answer
 
Have a look at the RegularExpressionValidator [^] class
 
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