How to limit txtbox num 0-9 and 1 spce btwn num with RegEx.

Everything else that doesn't fall into one of the other PB categories.
dcr3
Enthusiast
Enthusiast
Posts: 165
Joined: Fri Aug 04, 2017 11:03 pm

How to limit txtbox num 0-9 and 1 spce btwn num with RegEx.

Post by dcr3 »

Hi. I want to limit, a Textgadget with numbers from 0-9 and a single
space between numbers, with a RegularExpression like this "^\d[\d ]*\d$"
or any other RegularExpression for that matter. On a #PB_EventType_Change.

The code below is for illustration purposes only.

Code: Select all

Enumeration
  #str1
  #bt1
  #txt1
  #win
  #txt2
  #Font_win 
EndEnumeration
LoadFont(#Font_win, "", 10, #PB_Font_Bold)
Procedure Numbrs0T9wSpaces(nums.s)
 
   CreateRegularExpression(1, "^\d[\d ]*\d$")
   ;Answr = MatchRegularExpression(1,GetGadgetText(#str1))
   Answr = MatchRegularExpression(1,nums)
   ProcedureReturn Answr
  
  EndProcedure
  
  flg=#PB_Window_SystemMenu|#PB_Window_ScreenCentered
  OpenWindow(#win,0,0,310,160,"Limit Txtgadgt [0-9] and one space btwn nums",flg)
  SetWindowColor(#win,$C5BE58 )
  TextGadget(#txt1,30,25,240,20,"Country code and phone number ? ")
  SetGadgetColor(#txt1,#PB_Gadget_BackColor,$C5BE58)
  SetGadgetFont(#txt1, FontID(#Font_win))
  StringGadget(#str1,30,40,240,20,"44 766 7123  8384")
  TextGadget(#txt2,30,105,240,20,"",#PB_Text_Border|#PB_Text_Center)   
  SetGadgetFont(#txt2, FontID(#Font_win))
  ButtonGadget(#bt1,90,70,135,20,"CheckRegEx")
  SetGadgetFont(#bt1, FontID(#Font_win))
  Repeat 
      
      Select WaitWindowEvent()
       
      Case #PB_Event_CloseWindow
       
        appQuit = 1
        
        Case #PB_Event_Menu
        Select EventMenu()
         
        EndSelect
    
        Case #PB_Event_Gadget
         
        Select EventGadget()
              
          Case #bt1; here you suppose to use #str1
          
             L.s=" Letters or"
             Spces.s=" double spaces"
             CreateRegularExpression(1, "^\d[\d ]*\d$")
             Answr = MatchRegularExpression(1,GetGadgetText(#str1))
             
             ;If EventType() = #PB_EventType_Change
             
             If answr=#True
             SetGadgetColor(#txt2,#PB_Gadget_FrontColor, #Blue) 
             SetGadgetText(#txt2, "Correct")
             
             Else
             SetGadgetColor(#txt2,#PB_Gadget_FrontColor, #Red)
             SetGadgetText(#txt2, l+Spces+" Oh no!")
             
             EndIf
           
              ;???(EventGadget())
             
           ;EndIf
           
  ;this is what I am after           
  Debug Numbrs0T9wSpaces("44 766 7123 8384")
  
  Debug Numbrs0T9wSpaces("44 766 7123  8384")
  ;although this is #true I dont want doubles space
  
  Debug Numbrs0T9wSpaces(" 44 766 7123 8384")
  ;correct I don't want spaces in the begining
  
  Debug Numbrs0T9wSpaces("44 766 7123 8384 ")
  ;also correct I don't want spaces at the end

  Debug Numbrs0T9wSpaces("44 766 7123 8384c")
  ; correct I don't want letters
  Debug Numbrs0T9wSpaces("44 766 7123 ftreh")
  ;    "    "  "     "      "
  Debug Numbrs0T9wSpaces("d44 766 7123 8384")
  ;    "    "  "     "      "
       
         EndSelect
                 
    EndSelect 
   
  Until appQuit
  
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: How to limit txtbox num 0-9 and 1 spce btwn num with Reg

Post by Marc56us »

In other words
Begin with
digits
one space
digits
one space
digits
one space
digits
End

Code: Select all

^\d+[ ]{1}\d+[ ]{1}\d+[ ]{1}\d+$
But phone number with country code can be prefix by + and must follow with 2 digits

Code: Select all

^[+]*\d{2}[ ]{1}\d+[ ]{1}\d+[ ]{1}\d+$
Some user can fill phone numbers with . ou - or nothing

Code: Select all

^[+]*\d{2}[ .-]?\d+[ .-]?\d+[ .-]?\d+$
It is not the most optimal expression, but it is the easiest to read and understand.

:wink:
dcr3
Enthusiast
Enthusiast
Posts: 165
Joined: Fri Aug 04, 2017 11:03 pm

Re: How to limit txtbox num 0-9 and 1 spce btwn num with Reg

Post by dcr3 »

Thanks. Marc56us :)

I will use this RegEx "^\d+[ ]{1}\d+[ ]{1}\d+[ ]{1}\d+$" for know.
As far as the prefix by + or . and - to fill the Textgadget.
I will use a GadgetToolTip with an example.

My issue know, is how to use this in the EventType() as the user
types in the numbers in the GadgetText.

Code: Select all


Case #str1
; I used the ButtonGadget(#bt1) for check purposes only.
; in the code on my 1st post.

; What I really need is to use Textgadget(#str1) as self check.
		  
If EventType() = #PB_EventType_Change

 ;?? Numbrs0T9wSpaces(EventGadget());??? logic here  :(  any :idea: 
 
EndIf

Post Reply