2021年3月24日星期三

Validation of textbox that only allows numbers [closed]

I'm using Asp.net with C# (Visual Studio 2017).

  • I need to create a textbox that will ONLY allow 4 digits numbers, space key, and backspace. No special characters or alpha are allowed.
  • The range has to be between 1899 to current year.
  • AND it needs to validate that the years entered in the textbox are within 1899-2021.
  • AND i need a popup message saying "Invalid Year" when an incorrect year is entered in the textbox.

I have tried numerous methods but nothing is working. Does anyone have codes I can use and please be clear as to where I need to insert them.

The following is one set of sample code I've tried. However, for this one, I need the codes to only allow 4 digit numbers and a space key.

I also need to know where I should insert these codes. Hope this makes sense. Thank you!

<script type="text/javascript">    function isValidYear() {        var year = document.getElementById('tbxYear').value;        if (year > 2021 || year < 1899) {        alert("Range 1899 - 2021");        return false;      }      return true;    }  </script>      <asp:TextBox ID="tbxYear" runat="server" ClientIDMode="Static" type="number" min="1899" max="2021" step="1" onchange="isValidYear()" onpaste="return false;" BorderColor="Black" CausesValidation="True" CssClass="auto-style8" Font-Names="Arial" Height="25px"    MaxLength="4" ValidateRequestMode="Enabled" ValidationGroup="check" Width="147px"></asp:TextBox>&nbsp;<span class="auto-style16"><span class="auto-style15"><b><span style="font-size: small; font-family: Arial; color: #FF0000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
https://stackoverflow.com/questions/66790963/validation-of-textbox-that-only-allows-numbers March 25, 2021 at 07:37AM

没有评论:

发表评论