65.9K
CodeProject is changing. Read more.
Home

Validating Alphabets

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.09/5 (16 votes)

Jun 20, 2005

1 min read

viewsIcon

34455

downloadIcon

325

The function to validate whether the input is in alphabets or not.

Introduction

The purpose of writing this article is to share my knowledge with others. In this article, I have written the code for checking the alphabets.

Purpose

Though this is very basic level code but sometimes we need to include this type of code, in our applications. We can use this code where the entries should be in alphabets like ‘First Name’, ‘Last Name’, etc.

Functionality

The variable is passed as “C_S” as a parameter to the function named “Check_Alphabets”. There are two variables in the function:

  • Var_Loop
  • Var_Len

Var_Loop is for the looping purpose whereas Var_Len contains the length of “C_S”. Next, the contents in the C_S are being converted to small case by using the LCase function. Next each character in the C_S is being checked in the “For Next” loop. If any character in C_S is not in the range of 97 – 122 ASCII characters then the function returns false. 97 is the ASCII of ‘a’ whereas 122 is the ASCII of ‘z’. If all the characters are in the range of 97 – 122 ASCII characters then the function returns true.

Source Code

Note: Please download source file for proper functioning of the code.

Sub Btn_Submit_OnClick(Sender As Object, E As EventArgs)
  Dim Var_Txt_Text As String
  Var_Txt_Text = Trim(Txt_Text.Text)
  If Var_Txt_Text = "" Then
   Lbl_Message.Text = "Please enter something in the text box."
   Exit Sub
  ElseIf Check_Alphabets(Var_Txt_Text) = True Then
   Lbl_Message.Text = "Congratulations! Input is in alphabets."
  Else
   Lbl_Message.Text = "Sorry! Input is not in alphabets."
  End If
 End Sub
 
 Function Check_Alphabets(C_S)
  Dim Var_Loop, Var_Len
  Var_Len = Len(C_S)
  C_S = LCase(C_S)
  For Var_Loop = 1 To Var_Len
   If Mid(C_S, Var_Loop, 1) < Chr(97) Or Mid(C_S, Var_Loop, 1) > Chr(122) Then
    Return False
   End If
  Next
  Return True
 End Function