Introduction
Sometimes, we need to validate user input before or after they enter the value in a textbox or any other control on a page. At that time, we can check the below conditions.
Below are some sample conditions with functions and Key Code and Regular Expression respectively.
Let's start with the Key Code first.
Key Code
Suppose, we have a condition that we have allowed only Characters and Tab.
- <asp:TextBox ID="txtname" Width="200px" runat="server" MaxLength="250"
- onkeyup="Javascript:
();" Style="max-width: 200px; max-height: 80px; min-width: 200px; min-height: 80px;" onkeypress="return IsCharacterNumber(event,this.value.length <= 250);"></asp:TextBox>IsCharacter
- function IsCharacter(e) {
- var charCode = (e.which) ? e.which : e.keyCode;
- if (!(charCode >= 65 && charCode <= 90) && !(charCode >= 97 && charCode <= 122) && (charCode != 32 && charCode != 8) && !(charCode == 9)) {
- return false;
- }
- return true;
- }
When we have a condition that we have allowed only an IP Address.
- function IsCheckIP(e) {
- var charCode = (e.which) ? e.which : e.keyCode;
- if (charCode != 58 && charCode != 47 && charCode != 46 && (!(charCode >= 65 && charCode <= 90)) && (!(charCode >= 97 && charCode <= 122)) && (!(charCode >= 48 && charCode <= 57)) && (charCode != 8) && !(charCode == 9)) {
- return false;
- }
- return true;
- }
When you have allowed only Numeric values and Tab.
- function Numeric(e) {
- var charCode = (e.which) ? e.which : e.keyCode;
- if (!(charCode >= 48 && charCode <= 57) && !(charCode == 9)) {
- return false;
- }
- return true;
- }
Suppose, we have a condition that you have allowed only Characters, Numbers, and Tab.
- function IsCharacterNumber(e) {
- var charCode = (e.which) ? e.which : e.keyCode;
- if (!(charCode >= 65 && charCode <= 90) && !(charCode >= 97 && charCode <= 122) && (charCode != 32 && charCode != 8) && !(charCode >= 48 && charCode <= 57) && !(charCode == 9)) {
- return false;
- }
- return true;
- }
Suppose, we have a condition that you allowed only Character, Numbers, Underscore, and Tab
- function IsCharacterNumberUnderScore(e) {
- var charCode = (e.which) ? e.which : e.keyCode;
- if (!(charCode >= 65 && charCode <= 90) && !(charCode >= 97 && charCode <= 122) && (charCode != 32 && charCode != 8) && !(charCode >= 48 && charCode <= 57) && !(charCode == 95) && !(charCode == 9)) {
- return false;
- }
- return true;
- }
Sometimes, we need to find how many characters are left to enter. So that time, we can use the below code.
"Javascript:Remark();"
This line calls the Remark function,

Join the conversation! Your thoughts help the community grow.