cyberfly
3/31/2020 - 11:05 AM

Ruby validate name contains special characters

    def validate_name

        if @worker_name.blank?
            @errors.push({:code => -10, :message => 'Worker Name is empty'})
            return
        end

        if @worker_name.length > 50
            @errors.push({:code => -11, :message => 'Worker Name must not more than 50 characters'})
        end

        # check special chars

        unless @worker_name.match('^[a-zA-z]+([\s][a-zA-Z]+)*$')
            @errors.push({:code => -12, :message => 'Worker Name must not contains special characters'})
        end

    end