Form Validation (I want to restrict special characters in 1 of my form filed)

Form Validation (I want to restrict special characters in 1 of my form filed)

by Monika Gujar -
Number of replies: 9
Specially I don't want user to type '_' or space in field named 'Live Session Name'.
As it's creating problem to generate recording file.
Below is the code :

$mform->addElement('text', 'name', get_string('live_session_name', 'tgwhiteboard'));
        $mform->setType('name', PARAM_TEXT);
        $mform->addRule('name', null, 'required', null, 'client');
Average of ratings: -
In reply to Monika Gujar

Re: Form Validation (I want to restrict special characters in 1 of my form filed)

by Darko Miletić -
You should use regex Rule and set the desired valid text. For example

$mform->addRule('fieldname', 'sometext', 'regex', '^[^-]+$', 'server');

Add any other characters you do not wish to have in the text.

Average of ratings: Useful (1)
In reply to Darko Miletić

Re: Form Validation (I want to restrict special characters in 1 of my form filed)

by Monika Gujar -
Thank you so much.
But it's not working.

Below is the error. And form data is not getting saved.

Warning: preg_match(): Unknown modifier '-' in C:\xampp\htdocs\moodle\lib\pear\HTML\QuickForm\Rule\Regex.php on line 61


It would be great if I will be able to accept only characters and numbers (without space and any special character).

Below is my code :

$mform->addElement('text', 'name', get_string('live_session_name', 'tgwhiteboard'));
        $mform->setType('name', PARAM_TEXT);
        $mform->addRule('name', 'Live Session Name', 'required', null, 'client');   
        $mform->addRule('name', 'Field should contain only characters and numbers.', 'regex', '^[^-]+$', 'server');
In reply to Monika Gujar

Re: Form Validation (I want to restrict special characters in 1 of my form filed)

by Angel - -
The regex need delimiters, try with '/^[^_ ]+$/'

$mform->addRule('name', 'Field should contain only characters and numbers.', 'regex', '/^[^_ ]+$/', 'server');
Average of ratings: Useful (1)
In reply to Angel -

Re: Form Validation (I want to restrict special characters in 1 of my form filed)

by Aruna Kathiriya -

I added

$mform->addRule('name', 'Field should contain only characters and numbers.', 'regex',  '/^[^_ ]+$/', 'server');

But still it accepts the special chars.

Am I doing any mistake?



In reply to Aruna Kathiriya

Re: Form Validation (I want to restrict special characters in 1 of my form filed)

by Mark Johnson -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

As Darko said, "Add any other characters you do not wish to have in the text". The regex as written here will allow any character that's not an underscore. If you need to restrict other characters, you'll need to add them to the excluded class.

In general, if you are going to include regular expressions in your code, I would highly reccommend getting a good understanding of how they work and what they mean, not just copy/pasting blindly, as they are very powerful and easy to get wrong. Regexr is a helpful tool for explaining and testing expressions.

In reply to Mark Johnson

Re: Form Validation (I want to restrict special characters in 1 of my form filed)

by Aruna Kathiriya -
Yes Mark, you are right. But I have used

$mform->addRule('name', 'Field should contain only characters and numbers.', 'regex', '\'^£$%&*()}{@#~?><>,|=_+¬-]\', 'server');

But it didn't work either. I used http://buildregex.com/ to build my regex and used https://www.freeformatter.com/regex-tester.html to test. It workes fine on this site, but didn't gave any effect on Moodle.

Then I added "pattern" in HTML .
I changed
$mform->addElement('text', $field, get_string($field), 'maxlength="100" size="30" ');
to
$mform->addElement('text', $field, get_string($field), 'maxlength="100" size="30" pattern="[A-Za-z0-9]+"');

This solved my issue.
Thanks for your reply.
In reply to Monika Gujar

Re: Form Validation (I want to restrict special characters in 1 of my form filed)

by Vitaly Potenko -
Picture of Core developers Picture of Plugin developers
Just FYI if you don't want to mess with the suggested regex'es from the replies here you can implement the 'validate()' method of your form and process the desired field there by using the string functions.
In reply to Vitaly Potenko

Re: Form Validation (I want to restrict special characters in 1 of my form filed)

by Aruna Kathiriya -
Thanks Vitaly,
I used "pattern" in HTML statement.
I changed
$mform->addElement('text', $field, get_string($field), 'maxlength="100" size="30" ');
to
$mform->addElement('text', $field, get_string($field), 'maxlength="100" size="30" pattern="[A-Za-z0-9]+"');

This solved my issue.
Thanks for your reply.
Average of ratings: Useful (1)
In reply to Aruna Kathiriya

Re: Form Validation (I want to restrict special characters in 1 of my form filed)

by Saema Miftah -
Thanks it worked for me too!