classpredicates
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?
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 charactersOK={(C):-C=315orC=8or(C>=48andC<=57)},ifOK(Char)then% HOWTO 2017-02-15 block input of non-numeric characters!else
fail
end if.
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.
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 charactersOK={(C):-C=315orC=8or(C>=48andC<=57)},ifOK(Char)then% HOWTO 2017-02-15 block input of non-numeric characters!else
fail
end if.
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.
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 charactersOK={(C):-C=315orC=8or(C>=48andC<=57)},ifOK(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
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?
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?