Page 1 of 2

Visual Prolog : Edit Control : Check if string contains numbers

Posted: 6 Mar 2017 16:43
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?

Posted: 6 Mar 2017 18:18
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.

Posted: 7 Mar 2017 5:57
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'

Posted: 7 Mar 2017 14:08
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.

Posted: 7 Mar 2017 14:20
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.

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

Posted: 7 Mar 2017 16:25
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?

Posted: 7 Mar 2017 16:46
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?

Posted: 7 Mar 2017 17:00
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.

Posted: 7 Mar 2017 17:24
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'

Posted: 7 Mar 2017 17:28
by Gukalov
Ups)))
sorry
Just
if string:hasAlpha(Control:getText()) then

Posted: 7 Mar 2017 17:34
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

Posted: 7 Mar 2017 18:25
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" ?!)))

Re:

Posted: 7 Mar 2017 20:32
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

Posted: 7 Mar 2017 21:05
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?

Re:

Posted: 8 Mar 2017 4:27
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?