1

I'm trying to make a javascript call in a text box as shown bellow

<asp:TextBox ID='Textbox1' runat='server'></asp:TextBox>

I need to make the user input in such a way that user cannot put more than 9 digit before point and 9 digits after point. For example 123456789.123456789 this is the maximum length that a user can enter. I need to do it in keypress or keyup event. Please help me how to do it.

Rahul Chakrabarty
  • 2,149
  • 7
  • 39
  • 70

3 Answers3

0

Why don't you use the or the AJAX Control Toolkit Masked Edit Extender? For the first, your regular expression would look like this: ^\d{1,9}(\.{1,9})?$. You apply it like this:

<asp:TextBox ID='Textbox1' runat='server'></asp:TextBox>
<asp:RegularExpressionValidator runat="server" ControlToValidate="Textbox1" 
     ErrorMessage="Invalid format" ValidationExpression="^\d{1,9}(\.{1,9})?$" 
     SetFocusOnError="true"/>
शेखर
  • 17,412
  • 13
  • 61
  • 117
Ricardo Peres
  • 13,724
  • 5
  • 57
  • 74
0

You can use JQuery to do that.

Syntax:

$('#textboxID').on('KeyUp',function({
    // Validation Logic
}));

But I would suggest using KeyPress event instead of KeyUp event. Following is the case that might sneak if you use KeyUp

Case:

User has Input 123456789 and when he presses 10th key, textbox will show 1234567890. Once the keyUp is triggered, the validation will trigger and the text wil be reverted to 123456789. Though it will appear for a fraction of time, its better to be proactive than reactive.

References:

Community
  • 1
  • 1
Rajesh
  • 24,354
  • 5
  • 48
  • 79
0

Beside the JQuery to make sure that only 9 chars are in that text box you can do this

<asp:TextBox runat="server" MaxLength="9"></asp:TextBox>

May not be the answer but it is an alternative which in my opinion is a little easier.

Jonny
  • 401
  • 2
  • 11
  • As @Riki has mentioned he wants max 9 digits on both sides of decimal, max length will not solve issue. You can set it to 19 but still it will not help. – Rajesh Dec 04 '14 at 09:45