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 »

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: 453
Joined: 5 Nov 2000 0:01

Unread post by Harrison Pratt »

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 »

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: 453
Joined: 5 Nov 2000 0:01

Unread post by Harrison Pratt »

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 »

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
VIP Member
Posts: 68
Joined: 5 Oct 2011 15:16

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

Unread post by Gukalov »

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 »

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
VIP Member
Posts: 68
Joined: 5 Oct 2011 15:16

Unread post by Gukalov »

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 »

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
VIP Member
Posts: 68
Joined: 5 Oct 2011 15:16

Unread post by Gukalov »

Ups)))
sorry
Just
if string:hasAlpha(Control:getText()) then
Amod Gokhale
Posts: 8
Joined: 6 Mar 2017 16:40

Unread post by Amod Gokhale »

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
VIP Member
Posts: 68
Joined: 5 Oct 2011 15:16

Unread post by Gukalov »

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: 453
Joined: 5 Nov 2000 0:01

Re:

Unread post by Harrison Pratt »

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
VIP Member
Posts: 68
Joined: 5 Oct 2011 15:16

Unread post by Gukalov »

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: 453
Joined: 5 Nov 2000 0:01

Re:

Unread post by Harrison Pratt »

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