Click here to Skip to main content
16,016,345 members
Articles / Web Development / ASP.NET
Article

Validating Alphabets

Rate me:
Please Sign up or sign in to vote.
1.09/5 (24 votes)
19 Jun 20051 min read 34.3K   13   6
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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Pakistan Pakistan
I am a computer science graduate. I have been working in ASP.Net since January 2003.

Comments and Discussions

 
GeneralAn idea.... Pin
je_gonzalez20-Jun-05 19:31
je_gonzalez20-Jun-05 19:31 
Try using Regular Expressions...

In C#:

!System.Text.RegularExpressions.Regex.Match(
"the text to match",
"[^a-z]",
System.Text.RegularExpressions.RegexOptions.IgnoreCase
).Success

returns true if "the text to match" is all alpha....

By the way, we all got something to learn in this world ... Keep on learning.
GeneralRe: An idea.... Pin
Issa Qandil30-Jun-05 4:35
Issa Qandil30-Jun-05 4:35 
QuestionAre you serious? Pin
mav.northwind20-Jun-05 0:29
mav.northwind20-Jun-05 0:29 
AnswerRe: Are you serious? Pin
kaangonel20-Jun-05 1:34
kaangonel20-Jun-05 1:34 
GeneralRe: Are you serious? Pin
mav.northwind20-Jun-05 2:03
mav.northwind20-Jun-05 2:03 
AnswerRe: Are you serious? Pin
ilikc0de12-Jun-06 12:46
ilikc0de12-Jun-06 12:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.