Not allowing textarea to proceed if enter or space is entered

Notice: This thread is very old.
McLel
Member | 12
+
0
-

Hi,

how do I prevent textarea from proceeding when user enters Space(s) or Enter(s) into the field?

My code:

$form->addTextArea('comment', 'Comment:')
   ->setAttribute('placeholder', 'Comment')
   ->addRule(Form::FILLED, 'The field Comment cannot be empty');
Pavel Kravčík
Member | 1016
+
-1
-

You can try custom calback $form->onValidate[] = function() or something like this:

$form->addText('nick', 'Username')
	->addRule([$this, 'isUsernameAvailable'], 'Go home, this nickname is already taken');

//...

public function isUsernameAvaible()
{
	public function isUsernameAvailable($name)
    {
        //Your logic return Boolean
    }
}
David Matějka
Moderator | 6352
+
0
-

You can use a PATTERN validator, e.g:

->addRule(Form::PATTERN, 'error message', '[^\s]+')
McLel
Member | 12
+
0
-

David Matějka wrote:

You can use a PATTERN validator, e.g:

->addRule(Form::PATTERN, 'error message', '[^\s]+')

Thank you for the response. I should've made my question more clear:

I want error message to pop if user tries to enter Space(s) or Enter(s), but I want the textarea to proceed if he enters normal sentence(s).

In other words, if user enters just Space(s) or Enter(s), validation should consider textarea to be empty.

Last edited by McLel (2015-09-30 15:21)

David Matějka
Moderator | 6352
+
0
-

Oh, sorry. this regexp should work:

.*[^\s].*
McLel
Member | 12
+
0
-

David Matějka wrote:

Oh, sorry. this regexp should work:

.*[^\s].*

Great, I believe we are getting close :-). It returns error if spaces or new lines are entered and proceeds if sentences are entered. I need just one more thing.

I can now send “Hello world” but cannot send

Hello world

Hello there

How can I achieve this?

David Matějka
Moderator | 6352
+
0
-

Try negative validator using ~:

->addRule(~Form::PATTERN, '...', '[\s]+')
McLel
Member | 12
+
0
-

David Matějka wrote:

Try negative validator using ~:

->addRule(~Form::PATTERN, '...', '[\s]+')

Cool, this seems to be it. Thanks.