Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

[solved]Check if variable contains letters/numbers etc..


  • Please log in to reply
14 replies to this topic
ShadowfoxXXX
  • Members
  • 9 posts
  • Last active: Apr 22 2009 10:04 PM
  • Joined: 20 Apr 2009
I have a program that takes in an area code.
I need a way to check if it contains anything other than numbers.

[ Moderator!: Do not use all caps in the subject line "CHECK IF VARIABLE CONTAINS LETTERS, NUMBERS, ETC..." ]

Slanter
  • Members
  • 739 posts
  • Last active: Jul 08 2011 05:26 AM
  • Joined: 28 May 2008
MyVar = 123abc

If MyVar is not digit

   MsgBox MyVar contains something other than the numbers 0-9!



; Or

MyVar2 = 123456

If MyVar2 is digit

   MsgBox MyVar2 contains only the numbers 0-9!

Unless otherwise stated, all code is untested

(\__/) This is Bunny.
(='.'=) Cut, copy, and paste bunny onto your sig.
(")_(") Help Bunny gain World Domination.

keybored
  • Members
  • 351 posts
  • Last active: Apr 26 2013 09:08 AM
  • Joined: 18 Jun 2006
ShadowfoxXXX,
First read about how to get answers effectively.
http://www.autohotke...opic.php?t=4986
Then check the command list for "If var is [not] type"

ShadowfoxXXX
  • Members
  • 9 posts
  • Last active: Apr 22 2009 10:04 PM
  • Joined: 20 Apr 2009
ok... I read it.
I'm assuming you're trying to say I did something incorrectly?
I did check all over the place, and i found nothing.
I don't need to include any further information,
and the "If var is [not] type" didn't show up anywhere, because AHK has no explicitly defined variable types. They're all just variables. No ints, strgins, or booleans.
I need a way to analyze the contents of a variable, even if it reads one character at a time, and then report if it contains any letters.




I tried:

If AreaCode is not digit {
MsgBox Area Code contains something other than the numbers 0-9!
Gui, Show
}

with and without % around AreaCode. It didnt work. At all

VxE
  • Moderators
  • 3622 posts
  • Last active: Dec 24 2015 02:21 AM
  • Joined: 07 Oct 2006
Link to docs: If vars is [not] type

Braces are used incorrectly here.

If AreaCode is not digit {
   MsgBox Area Code contains something other than the numbers 0-9!
   Gui, Show
}


this is correct:
If AreaCode is not digit
{ ; OTB style is only supported in some cases
   MsgBox Area Code contains something other than the numbers 0-9!
   Gui, Show
}

Or with regex:
InputBox, userinput, input a number
If !RegexMatch( userinput, "[^0-9]" ) && userinput != ""
	msgbox, You entered nothing but numbers
else
	msgbox, You entered something that wasn't a number


rulfzid
  • Members
  • 62 posts
  • Last active: Mar 11 2011 08:31 PM
  • Joined: 27 Nov 2008
Another method that I find useful:

InputBox, userinput, input a number
If userinput+0 ; only evaluates to true if userinput is a number
   msgbox, You entered nothing but numbers
else
   msgbox, You entered something that wasn't a number


Krogdor
  • Members
  • 1391 posts
  • Last active: Jun 08 2011 05:31 AM
  • Joined: 18 Apr 2008

Another method that I find useful:


That method will also accept hexadecimal values, though, which in this case would not be desirable.

ShadowfoxXXX
  • Members
  • 9 posts
  • Last active: Apr 22 2009 10:04 PM
  • Joined: 20 Apr 2009
braces used this way

if (blablabl){
stuff here
}

has always worked for me.
in fact i used it multiple times in this program and it worked fine.

somehow that particular method only works when i change it.
I guess i gotta get off the whole java thing.

Krogdor
  • Members
  • 1391 posts
  • Last active: Jun 08 2011 05:31 AM
  • Joined: 18 Apr 2008
OTB style, in which the brace is placed on the same line as the if-statement, is only valid for if-statements that are expressions. Normal if-statements such as
If SomeVar = SomeString
or the special if-statements such as
If SomeVar is [not] type
and
If SomeVar [not] in/contains MatchList
do NOT support OTB, and must have the brace appear on the next line.

ShadowfoxXXX
  • Members
  • 9 posts
  • Last active: Apr 22 2009 10:04 PM
  • Joined: 20 Apr 2009
a a

ShadowfoxXXX
  • Members
  • 9 posts
  • Last active: Apr 22 2009 10:04 PM
  • Joined: 20 Apr 2009
so what if i wanted to check for numbers?
and symbols?
is it
if blank is not letter
if blank is not string

what is it?

Krogdor
  • Members
  • 1391 posts
  • Last active: Jun 08 2011 05:31 AM
  • Joined: 18 Apr 2008
Please, at the very least, read what has been posted in your thread. [VxE] has already posted a link to the help file, where you will find all of the information you need.

Link to docs: If vars is [not] type



keybored
  • Members
  • 351 posts
  • Last active: Apr 26 2013 09:08 AM
  • Joined: 18 Jun 2006

so what if i wanted to check for numbers?
and symbols?...


Please give some examples of the variables you are evaluating. I thought you were looking for just numbers, guess not.

The initial direction to read the "faq" on how to get answers was because you posted no code. Had you done so it might have provided some examples of what you were evaluating. Looks like we're making progress. Hang in there.

EvilleMonkey
  • Members
  • 39 posts
  • Last active: Jul 15 2016 01:52 PM
  • Joined: 04 Feb 2014

Rather than start a new topic I hope its ok to post my question here since its related....

 

I want to check a variable for characters only BUT it needs to allow for spaces, so, 

If not alpha

Will not work if it contains a space. Any suggestions?



kon
  • Members
  • 1652 posts
  • Last active:
  • Joined: 04 Mar 2013

I want to check a variable for characters only BUT it needs to allow for spaces, so, 

If not alpha

Will not work if it contains a space. Any suggestions?

One way:

; If Var contains any characters that are NOT A-Z or a space (case insensitive)
if (RegExMatch(Var, "i)[^A-Z ]"))

https://regex101.com/r/aN8mZ9/1