Digital Workshop

Welcome to the Digital Workshop Message Boards
It is currently October 5th, 2024, 4:19 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: var contains a number question...
PostPosted: January 28th, 2005, 2:29 pm 
Offline

Joined: December 14th, 2004, 5:08 pm
Posts: 55
Location: Holland
Opus: version 8.0
OS: XP, Vista, Windows7, Windows8
System: Intel Corei7-2600 CPU 3.400GHz
How can I check if a variable contains a number instead of text?

if (checkVar contains a number){
action}
else{
action}

e.g.
var checkVar = 10052367A
will give a false result
var checkVar=125894
will giva a false result
var checkVar=ABC
will give a true result

Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: January 28th, 2005, 2:51 pm 
Offline

Joined: October 25th, 2004, 12:27 pm
Posts: 526
Location: Digital Workshop
Hi,

Thank you for your enquiry.

I'm sure there is an easier way of achieving this, but the following script actions should hopefully suffice.

Code:
count = 0
len = input.length
for (n=0;n<len;n++) {
   current = input.charAt(n)
   if (current >= "0" && current <= "9") {
      count++
   }
}

if (count > 0) {
   Debug.trace("Contains numbers")
   } else {
   Debug.trace("No numbers")
}


where input is the variable you are trying to check. You can obviously replace the first Debug.trace() function with whatever actions you want to take place when the string contains numerical values, and replace the second Debug.trace() function with the actions that should occur when the variable contains no numbers.

Kind regards,

_________________
Robin Garrett
Digital Workshop Technical Support


Top
 Profile Visit website  
 
 Post subject: Var and number question
PostPosted: January 29th, 2005, 11:31 pm 
Offline

Joined: November 3rd, 2004, 2:11 pm
Posts: 323
Here is another way of doing it:

Result would be a variable in your Opus publication that might be in a popup box to indicate to your user that numbers are not valid. One other thing I was thinking about is, could you not just specify if you are using an input field not to use numbers?


var iChars = "0123456789"

for (var i = 0; i < test.length; i++)
{
if (iChars.indexOf(test.charAt(i)) == -1){
result="All letters";
}
else {
result="You have a number here.";
}
}


Top
 Profile Visit website  
 
 Post subject:
PostPosted: January 30th, 2005, 11:56 am 
Offline

Joined: December 14th, 2004, 5:08 pm
Posts: 55
Location: Holland
Opus: version 8.0
OS: XP, Vista, Windows7, Windows8
System: Intel Corei7-2600 CPU 3.400GHz
Both are working like a charm...
Thank you


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group