PHP Nette - form validation - REGEX rule for czech characters + space and dash
Hi,
I have a form with a name input in PHP Nette project, which is validated by REGEX rule:
For typical Czech names it is satisfactory, but for two names or names with a dash it does not suit. How should I modify the REGEX rule so that there can be a space and a dash?
Thanks
Hi,
so that you have a regular vessel in the Nette form, even for a dash, you could adjust REGEX as follows:
REGEX - may contain a dash (minus)
REGEX - space
You could be happy with this definition of the form with this regular:
But the best thing to do is this regular expression (it goes through multiple names in a row), so the resume is:
I have a form with a name input in PHP Nette project, which is validated by REGEX rule:
$form->addText('name', 'Jméno')
->setHtmlAttribute('class', 'form-control')
->addRule(Form::MIN_LENGTH, 'Jméno musí mít alespoň %d znaky', 2)
->addRule(Form::MAX_LENGTH, 'Jméno nemůže mít více jak %d znaků', 20)
->addRule(Form::PATTERN, 'Jméno obsahuje nepovolené znaky.', '[a-zA-ZáčďéěíňóřšťůúýžÁČĎÉĚÍŇÓŘŠŤŮÚÝŽ]+[ \-]?')
->setRequired('Zadejte jméno');
For typical Czech names it is satisfactory, but for two names or names with a dash it does not suit. How should I modify the REGEX rule so that there can be a space and a dash?
Thanks
REPLY
Hi,
so that you have a regular vessel in the Nette form, even for a dash, you could adjust REGEX as follows:
REGEX - may contain a dash (minus)
[a-zA-ZáčďéěíňóřšťůúýžÁČĎÉĚÍŇÓŘŠŤŮÚÝŽ-]
REGEX - space
+[\w ]
You could be happy with this definition of the form with this regular:
$form->addText('name', 'Jméno')
->setHtmlAttribute('class', 'form-control')
->addRule(Form::MIN_LENGTH, 'Jméno musí mít alespoň %d znaky', 2)
->addRule(Form::MAX_LENGTH, 'Jméno nemůže mít více jak %d znaků', 20)
->addRule(Form::PATTERN, 'Jméno obsahuje nepovolené znaky.', '[a-zA-ZáčďéěíňóřšťůúýžÁČĎÉĚÍŇÓŘŠŤŮÚÝŽ-]+[\w ]+[ \-]?')
->setRequired('Zadejte jméno');
But the best thing to do is this regular expression (it goes through multiple names in a row), so the resume is:
$form->addText('name', 'Jméno')
->setHtmlAttribute('class', 'form-control')
->addRule(Form::MIN_LENGTH, 'Jméno musí mít alespoň %d znaky', 2)
->addRule(Form::MAX_LENGTH, 'Jméno nemůže mít více jak %d znaků', 20)
->addRule(Form::PATTERN, 'Jméno obsahuje nepovolené znaky.', '[ ./a-zA-ZáčďéěíňóřšťůúýžÁČĎÉĚÍŇÓŘŠŤŮÚÝŽ-]+[ \-]?')
->setRequired('Zadejte jméno');