how to check character in between 0 to 9 in asp

Dani AI

Generated

Quick correction and a compact summary of practical options. started with Asc(Mid(...)) but used the ASCII range for letters; digits 0–9 are ASCII codes 48–57 (uppercase A–Z are 65–90, lowercase a–z are 97–122). ’s IsNumeric approach is useful for numeric-range checks but accepts signs/decimals (so it’s not a strict “single digit” test). is right that RegExp is often the cleanest way to validate a whole string.

Fast, idiomatic single-character check (VBScript Like — simplest for one char):

' get a single character (Mid uses 1-based positions)
ch = Mid(myStr, pos, 1)

If ch Like "#" Then
  ' ch is a digit 0-9
Else
  ' not a digit
End If

ASCII-code alternative (explicit, fast, good when you want numeric ranges):

code = Asc(ch)   ' ch must be a single-character string
If code >= 48 And code <= 57 Then
  ' digit 0-9
End If

Whole-string validation (recommended if you need the entire input to be digits):

Set re = New RegExp
re.Pattern = "^[0-9]+$"
If re.Test(myStr) Then
  ' myStr contains only digits
End If

Notes and pitfalls: always Trim() input and check Len() > 0 before using Mid or Asc. IsNumeric is handy for numeric ranges but will accept "3.14", "+7" or currency formats — not ideal for single-digit checks. For multi-byte or non-ASCII input be cautious (the ASCII ranges above apply to standard digit characters). These options cover most classic-ASP needs: use Like "#" for a quick per-character test, Asc when you want explicit code checks, and RegExp for full-string validation.

Recommended Answers

All 4 Replies

<%@ Language=VBScript %>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
</HEAD>
<BODY>

<P>&nbsp;</P>
<%
dim i , j,chStr
chstr="12G43"

For i= 0 to (len(chstr)-1)

	if (Asc(mid(chStr,i+1,1)) > 64 and Asc(mid(chStr,i+1,1)) < 90)  then

		RESPONSE.WRITE mid(chStr,i+1,1) & "CHARACTER ENTRY" &  "<br />"
		Exit For
	else

		response.write mid(chStr,i+1,1) & "NUMBER ENTRY" &  "<br />"
	end if 

Next
response.write "Done"
%>
</BODY>
</HTML>
If IsNumeric(TheForm.Text1.Value) Then
    If TheForm.Text1.Value < 1 Or TheForm.Text1.Value > 10 Then
      MsgBox "Please enter a number between 1 and 10."
    Else
      MsgBox "Thank you."
    End If
  Else
    MsgBox "Please enter a numeric value."
  End If

Hi this code is working well and good.........

If u have doubts here regarding this code u can remind me...

Thank U

In many way you can check it..

Also you can check by

If chstr > 0 Then
' Your Code Heree...
End IF

If there is any one alpha character then the condition will be false if not then 100% all the numbers will be in between 0-9...

But Using RegExp is the better choice.

Okay Good.......
Thank U...

It is better to use here RegExp as U said like this .........

If ?U want to check Alphabetics also , U can use this code


regExp.Pattern = "[A-Z]|[a-z]|[0-9]"


set match = regExp.Execute(spec)


Thank U
Urs Venu.K

In many way you can check it..

Also you can check by

If chstr > 0 Then
' Your Code Heree...
End IF

If there is any one alpha character then the condition will be false if not then 100% all the numbers will be in between 0-9...

But Using RegExp is the better choice.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.