It is currently Wed Sep 18, 2019 6:15 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: How to limit txtbox num 0-9 and 1 spce btwn num with RegEx.
PostPosted: Sat Jul 21, 2018 12:28 pm 
Offline
User
User

Joined: Fri Aug 04, 2017 11:03 pm
Posts: 74
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:
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
 


Top
 Profile  
Reply with quote  
 Post subject: Re: How to limit txtbox num 0-9 and 1 spce btwn num with Reg
PostPosted: Sat Jul 21, 2018 2:06 pm 
Offline
Enthusiast
Enthusiast

Joined: Sat Feb 08, 2014 3:26 pm
Posts: 632
Location: France
In other words
Begin with
digits
one space
digits
one space
digits
one space
digits
End

Code:
^\d+[ ]{1}\d+[ ]{1}\d+[ ]{1}\d+$

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

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

It is not the most optimal expression, but it is the easiest to read and understand.

:wink:

_________________
English is not my native language, I use an online translator


Top
 Profile  
Reply with quote  
 Post subject: Re: How to limit txtbox num 0-9 and 1 spce btwn num with Reg
PostPosted: Sat Jul 21, 2018 5:41 pm 
Offline
User
User

Joined: Fri Aug 04, 2017 11:03 pm
Posts: 74
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:

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



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

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 16 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

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye