FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log inLog in 


Visual Prolog : Edit Control : Check if string contains numbers

Post new topic   Reply to topic    discuss.visual-prolog.com Forum Index -> Visual Prolog
View previous topic :: View next topic  
Author Message
Amod Gokhale



India
Joined: 06 Mar 2017
Posts: 8

PostPosted: 6 Mar 2017 16:43    Post subject: Visual Prolog : Edit Control : Check if string contains numbers Reply with quote

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

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?
Back to top
View user's profile Send private message
Harrison Pratt



Des Moines, Iowa USA
Joined: 05 Nov 2000
Posts: 290

PostPosted: 6 Mar 2017 18:18    Post subject: Reply with quote

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.


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.

Back to top
View user's profile Send private message
Amod Gokhale



India
Joined: 06 Mar 2017
Posts: 8

PostPosted: 7 Mar 2017 5:57    Post subject: Reply with quote

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'
Back to top
View user's profile Send private message
Harrison Pratt



Des Moines, Iowa USA
Joined: 05 Nov 2000
Posts: 290

PostPosted: 7 Mar 2017 14:08    Post subject: Reply with quote

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.

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

Back to top
View user's profile Send private message
Amod Gokhale



India
Joined: 06 Mar 2017
Posts: 8

PostPosted: 7 Mar 2017 14:20    Post subject: Reply with quote

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'

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.

Back to top
View user's profile Send private message
Gukalov



Orthez, France
Joined: 05 Oct 2011
Posts: 21

PostPosted: 7 Mar 2017 16:25    Post subject: Re: Visual Prolog : Edit Control : Check if string contains numbers Reply with quote

Hi.

Amod Gokhale wrote:

Is there example to validate if string contains only alphabets and message user if it contains numbers?

Class string

predicates
    hasAlpha : (string Source) determ.
    % @short Succeds if #Source only contains alphabetic characters.

It's not enough?

_________________
aagukalov@gmail.com
Back to top
View user's profile Send private message
Amod Gokhale



India
Joined: 06 Mar 2017
Posts: 8

PostPosted: 7 Mar 2017 16:46    Post subject: Reply with quote

Hi Gukalov,

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

Can you please post complete code?
Back to top
View user's profile Send private message
Gukalov



Orthez, France
Joined: 05 Oct 2011
Posts: 21

PostPosted: 7 Mar 2017 17:00    Post subject: Reply with quote

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.

_________________
aagukalov@gmail.com
Back to top
View user's profile Send private message
Amod Gokhale



India
Joined: 06 Mar 2017
Posts: 8

PostPosted: 7 Mar 2017 17:24    Post subject: Reply with quote

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'
Back to top
View user's profile Send private message
Gukalov



Orthez, France
Joined: 05 Oct 2011
Posts: 21

PostPosted: 7 Mar 2017 17:28    Post subject: Reply with quote

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

_________________
aagukalov@gmail.com
Back to top
View user's profile Send private message
Amod Gokhale



India
Joined: 06 Mar 2017
Posts: 8

PostPosted: 7 Mar 2017 17:34    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
Gukalov



Orthez, France
Joined: 05 Oct 2011
Posts: 21

PostPosted: 7 Mar 2017 18:25    Post subject: Reply with quote

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" ?!)))

_________________
aagukalov@gmail.com
Back to top
View user's profile Send private message
Harrison Pratt



Des Moines, Iowa USA
Joined: 05 Nov 2000
Posts: 290

PostPosted: 7 Mar 2017 20:32    Post subject: Re: Reply with quote

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'

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


Back to top
View user's profile Send private message
Gukalov



Orthez, France
Joined: 05 Oct 2011
Posts: 21

PostPosted: 7 Mar 2017 21:05    Post subject: Reply with quote

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?

_________________
aagukalov@gmail.com
Back to top
View user's profile Send private message
Harrison Pratt



Des Moines, Iowa USA
Joined: 05 Nov 2000
Posts: 290

PostPosted: 8 Mar 2017 4:27    Post subject: Re: Reply with quote

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?

Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    discuss.visual-prolog.com Forum Index -> Visual Prolog All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
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
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum