-1

I am creating a webpage (not windows form) where textbox should take only 10 numeric digits, This is not a duplicate question as

if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) &&
    (e.KeyChar != '.'))
{
        e.Handled = true;
}

giving error for missing reference assembly (This is only for Windows form). When I am setting TextBox property to numeric-only MAXLENGTH function is not working, and when setting to TEXT instead of numeric, MAXLENGTH is working but then it is accepting characters also. I don't want to check this while submitting the value, this should be checked when the user is entering a value in TextBOX. Appreciate any help/advice/comment.

  • 1
    When you run C# code on a webpage it runs on the asp.net server! Instead you should include some JavaScript on the page to handle this situation, that runs on the client. – Poul Bak Feb 12 '20 at 19:14
  • 1
    see [this](https://stackoverflow.com/a/9732876) answer, change the regex to `\d{10}` – Matt.G Feb 12 '20 at 21:08
  • @Matt.G Moment I change it even then the user is able to type more than 10 numbers (Error does show on Range validator , but the user can type.) However, I have got the solution and posted this. – usama.nomani Feb 13 '20 at 08:46

2 Answers2

0

I think this is what you're looking for TextBox.MaxLength Property

  • No man it limits the input only but will accept characters, I know how to limit the characters, My requirements are it should accept max 10 numeric value. I can't get this combo. – usama.nomani Feb 12 '20 at 19:07
-1

I have created two separate functions using JavaScript, code may not seem to be very optimal but it worked for me. Maybe someone who has good hands-on Java script can combine it.

    <SCRIPT language=Javascript>
        function isNumberKey(evt) {
            var charCode = (evt.which) ? evt.which : evt.keyCode;
            if (charCode > 31 && (charCode < 48 || charCode > 57))
                return false;
            return true;
        }
   </SCRIPT>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="MaxLength.min.js"></script>
<script type="text/javascript">
    $(function () {
        //Normal Configuration
        $("[id*=TextBox4]").MaxLength({ MaxLength: 10 });

    });
</script>

I hope this will help someone!