0

I want o validate asp.net text box control by using javascript for number entry only. And maxlength of data should be 3 digit. For this i used following script -

function isNumberKey(evt, obj) {

       var charCode = (evt.which) ? evt.which : event.keyCode
       var txt = obj.value.length;
       if (charCode > 31 && (charCode < 48 || charCode > 57))
           return false;
       else {
           if (txt < 3) {
               return true;
           }
           else {
               return false;
           }
       }
   }

and html code is as follows --

<asp:TextBox ID="txtNoCall" runat="server" onkeypress="javascript:return isNumberKey(event,this);"></asp:TextBox>

It is validating for numeric entry. and restrict length for 3 digit. but problem is that after 3 digit when i'm pressing backsapce key then that time it is not working.

How to solve this?

thanks.

Priyanka
  • 2,802
  • 14
  • 55
  • 88
  • 1
    is there a reason you're not using the built in asp.net validators and having them generate your client side code – Daniel Powell Nov 17 '11 at 05:17
  • @Daniel Powell:I don't want to generate any message which will be shown by the asp.net validators. And also it takes space from my web page too. – Priyanka Nov 17 '11 at 05:27
  • 1
    you can leave the error message blank and nothing will be shown to the user – Daniel Powell Nov 17 '11 at 05:31
  • 1
    its probably a good idea to give the user some kind of visual feedback anyway that they are entering something invalid – Daniel Powell Nov 17 '11 at 05:32
  • @Daniel Powell: when i'm using above javascript. Then i'm strictly validate the data. There is not need to show any validation related error message to the user,Which may be consume yours time. – Priyanka Nov 17 '11 at 05:46

3 Answers3

3

Using the inbuilt asp.net validators would be much easier and give you both server and client side validation

 <asp:RegularExpressionValidator id="RegularExpressionValidator1" 
                     ControlToValidate="txtNoCall"
                     ValidationExpression="\d{3}"
                     Display="Static"
                     ErrorMessage="Only 3 digits allowed"
                     EnableClientScript="True" 
                     runat="server"/>

Note I haven't run this but I had a feeling that enableclientscript didnt work on the regex validators but msdn documentation doesnt seem to mention anything about it so maybe I'm wrong.

Daniel Powell
  • 8,143
  • 11
  • 61
  • 108
0

You can do something like

if ((txt == 3) && (charCode == 8)) {
                obj.value = obj.value.toString().substring(0, txt-1);
            }

Here is complete code.

function isNumberKey(evt, obj) {
            var LIMIT = 3;           
            var charCode = (evt.which) ? evt.which : event.keyCode           
            var txt = obj.value.length;
            if ((txt == LIMIT) && (charCode == 8)) {
                obj.value = obj.value.toString().substring(0, txt-1);
            }
            if (charCode > 31 && (charCode < 48 || charCode > 57))
                return false;
            else {
                if (txt < LIMIT) {
                    return true;
                }
                else {
                    return false;
                }
            }  
        }
SaQiB
  • 639
  • 8
  • 18
0

Set TextBox.MaxLength as per the documentation.

If you need it to be Multiline, check out Specifying maxlength for multiline textbox.

If you try and build this yourself, make sure you include Ctrl-V and Shift-Ins in your test scenarios (most solutions don't handle these well).

As well as this, you should build the server side to do the validation there. It is impossible to solve the problem completely client side (since the user could always hand craft their own POST, or disable javascript).

Community
  • 1
  • 1
mjwills
  • 23,389
  • 6
  • 40
  • 63