visual-prolog

Visual Prolog : Edit Control : Check if string contains numbers


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?


Solution

  • Below code adds validation for allowing only alphabets. Replaceall allows with Spaces. Thanks to Gukalov for providing answer on discuss . visual-prolog . com

    class predicates
        allowonlyalphabets : control::validateResponder.
    clauses
        allowonlyalphabets(Control)  =
        if  string::hasAlpha(string::replaceAll(Control:getText(), " ", "")) then
            control::contentsOk
        else
            control::contentsInvalid(Control, Control,
                string::format("%s must not contain numbers!", Control:getLabel()))
        end if.