Visual Prolog : Edit Control : Check if string contains numbers

Discussions related to Visual Prolog
Amod Gokhale
Posts: 8
Joined: 6 Mar 2017 16:40

Visual Prolog : Edit Control : Check if string contains numbers

Unread post by Amod Gokhale » 6 Mar 2017 16:43

We are using below example to validate that edit control only contains numbers.

Code: Select all

class predicates     validateNumber : control::validateResponder. clauses     validateNumber(Control) = control::contentsOk :-         hasDomain(integer, _X),         _X = trytoTerm(Control:getText()),         !.     validateNumber(Control) = control::contentsInvalid(Control, Control,             string::format("%s must be an integer!", Control:getLabel())).
Is there example to validate if string contains only alphabets and message user if it contains numbers?

Harrison Pratt
VIP Member
Posts: 284
Joined: 5 Nov 2000 0:01

Unread post by Harrison Pratt » 6 Mar 2017 18:18

Amod,
I use a control's charResponder to block entry of "uninteresting" characters into the control when only simple character exclusion is desired. This seems a little more graceful that alerting the user with an error message after the text had been entered.

In the example below, onListEdit_ctl_PAGENUMChar is a control into which the user enters a page number.

Code: Select all

predicates     onListEdit_ctl_PAGENUMChar : window::charResponder. clauses     onListEdit_ctl_PAGENUMChar(_Source, Char, _ShiftControlAlt) = window::defaultCharHandling:-           %-- accepts only digits and backspace NOTE: backspace is a sequence of 315 and 8 characters         OK = { (C) :- C = 315                     or C = 8                     or (C >= 48 and C <= 57 )             },         if OK(Char) then  % HOWTO 2017-02-15  block input of non-numeric characters                 !             else                 fail         end if.

Amod Gokhale
Posts: 8
Joined: 6 Mar 2017 16:40

Unread post by Amod Gokhale » 7 Mar 2017 5:57

Hi Harrison,

Thanks for your reply. I tried running below program but got this error on fail

error c631 : The predicate 'demoForm::onNameChar/3-> (i,i,i)', which is declared as 'procedure', is actually 'determ'

Harrison Pratt
VIP Member
Posts: 284
Joined: 5 Nov 2000 0:01

Unread post by Harrison Pratt » 7 Mar 2017 14:08

Did you put the code into a charResponder created by the VP dialog editor? You will need to change _Char to Char in the generated code for the clause head.

Code: Select all

        OK = { (C) :- C = 315                     or C = 8                     or (C >= 48 and C <= 57 )             },         if OK(Char) then                   !             else                 fail         end if.

Amod Gokhale
Posts: 8
Joined: 6 Mar 2017 16:40

Unread post by Amod Gokhale » 7 Mar 2017 14:20

Hi Harrison,

Yes i did. it gives error on fail
error c631 : The predicate 'demoForm::onNameChar/3-> (i,i,i)', which is declared as 'procedure', is actually 'determ'

Code: Select all

predicates     onNameChar : window::charResponder. clauses     onNameChar(_Source, Char, _ShiftControlAlt) = window::defaultCharHandling:-           %-- accepts only digits and backspace NOTE: backspace is a sequence of 315 and 8 characters         OK = { (C) :- C = 315                     or C = 8                     or (C >= 48 and C <= 57 )             },         if OK(Char) then  % HOWTO 2017-02-15  block input of non-numeric characters                 !             else                   fail         end if.

User avatar
Gukalov
Active Member
Posts: 27
Joined: 5 Oct 2011 15:16

Re: Visual Prolog : Edit Control : Check if string contains numbers

Unread post by Gukalov » 7 Mar 2017 16:25

Hi.
Amod Gokhale wrote: Is there example to validate if string contains only alphabets and message user if it contains numbers?
Class string

Code: Select all

predicates     hasAlpha : (string Source) determ.     % @short Succeds if #Source only contains alphabetic characters.
It's not enough?

Amod Gokhale
Posts: 8
Joined: 6 Mar 2017 16:40

Unread post by Amod Gokhale » 7 Mar 2017 16:46

Hi Gukalov,

How to use this in class predicates
validateNumber : control::validateResponder.
clauses
validateNumber(Control) = control::contentsOk :-

Can you please post complete code?

User avatar
Gukalov
Active Member
Posts: 27
Joined: 5 Oct 2011 15:16

Unread post by Gukalov » 7 Mar 2017 17:00

As you did before:
clauses
validateNumber(Control) =
if _ = string:hasAlpha(Control:getText()) then
control::contentsOk
else
control::contentsInvalid(Control, Control, string::format("%s must be ???", Control:getText()))
end if.

Amod Gokhale
Posts: 8
Joined: 6 Mar 2017 16:40

Unread post by Amod Gokhale » 7 Mar 2017 17:24

i tried that on this line
if _ = string::hasAlpha(Control:getText()) then

it says error c502 : The expression does not produce a value

if _ = string:hasAlpha(Control:getText()) then

error i get is error c229: Undeclared identifier 'string'

User avatar
Gukalov
Active Member
Posts: 27
Joined: 5 Oct 2011 15:16

Unread post by Gukalov » 7 Mar 2017 17:28

Ups)))
sorry
Just
if string:hasAlpha(Control:getText()) then

Amod Gokhale
Posts: 8
Joined: 6 Mar 2017 16:40

Unread post by Amod Gokhale » 7 Mar 2017 17:34

Hi Gukalov,

Thank you so much!!!!!

Now it rejects numbers.. just another question. Now it doesn't allow spaces. Do I need to write another condition for that

e.g. Amod Gokhale is rejected but AmodGokhale works

User avatar
Gukalov
Active Member
Posts: 27
Joined: 5 Oct 2011 15:16

Unread post by Gukalov » 7 Mar 2017 18:25

Simplier to delete spaces before checking:
if string:hasAlpha(string::replaceAll(Control:getText(), " ", "")) then

But to check if entering name is correct you need something more sirious than just letters&spaces)))
"A m o d Go kh ale" ?!)))

Harrison Pratt
VIP Member
Posts: 284
Joined: 5 Nov 2000 0:01

Re:

Unread post by Harrison Pratt » 7 Mar 2017 20:32

Amos,
You need to add a second clause to handle the FAIL . See below.

Harrison
Amod Gokhale wrote:Hi Harrison,

Yes i did. it gives error on fail
error c631 : The predicate 'demoForm::onNameChar/3-> (i,i,i)', which is declared as 'procedure', is actually 'determ'

Code: Select all

predicates     onNameChar : window::charResponder. clauses     onNameChar(_Source, Char, _ShiftControlAlt) = window::defaultCharHandling:-           %-- accepts only digits and backspace NOTE: backspace is a sequence of 315 and 8 characters         OK = { (C) :- C = 315                     or C = 8                     or (C >= 48 and C <= 57 )             },         if OK(Char) then  % HOWTO 2017-02-15  block input of non-numeric characters                 !             else                   fail         end if.       onNameChar(_,_,_) = window::charHandled(). % <== ADD  THIS TO CATCH THE FAIL

User avatar
Gukalov
Active Member
Posts: 27
Joined: 5 Oct 2011 15:16

Unread post by Gukalov » 7 Mar 2017 21:05

Harrison, what about "Ctrl+V"?!
Looks like your code doesn't check what user pastes with Ctrl+V.
And user ain't happy if pasting prohibitted at all.
Isn't it?

Harrison Pratt
VIP Member
Posts: 284
Joined: 5 Nov 2000 0:01

Re:

Unread post by Harrison Pratt » 8 Mar 2017 4:27

Gukalov,
You can easily extend the allowable characters as needed -- I didn't need to paste in my particular application.
Gukalov wrote:Harrison, what about "Ctrl+V"?!
Looks like your code doesn't check what user pastes with Ctrl+V.
And user ain't happy if pasting prohibitted at all.
Isn't it?

Post Reply