Forum Discussion

John_Heyer_1508's avatar
John_Heyer_1508
Icon for Cirrostratus rankCirrostratus
Apr 20, 2017

Using String Match to Check if all Numbers

Basically I'm looking to test if a string is 10 digits and all numbers. Matching the 10 digits works, but not having any luck verifying if all numbers. This is the code I'm trying to use:

 

 set STRING "1234567890"
 if { [string length $STRING] == 10 && [string match {^[0-9]*} $STRING ] } {
   log "The string is 10 digits"
 }
  • Try this:

    when ACCESS_ACL_ALLOWED {
    
             set STRING "1234567890"
             if { [string length $STRING] == 10 && [string is digit $STRING ] == 1 } {
                log local0. "The string is 10 digits"
             } else {
                log local0. "The string is not 10 digits"
             }
        }
    
  • Try this:

    when ACCESS_ACL_ALLOWED {
    
             set STRING "1234567890"
             if { [string length $STRING] == 10 && [string is digit $STRING ] == 1 } {
                log local0. "The string is 10 digits"
             } else {
                log local0. "The string is not 10 digits"
             }
        }
    
    • John_Heyer_1508's avatar
      John_Heyer_1508
      Icon for Cirrostratus rankCirrostratus

      Thanks! "is digit" is the shortcut I was looking for. I had been trying "string is integer", but in order for that to work I would have had to type it to int first, which seemed like an unnecessary step.