I want to add validation to the node title field. When the user enters the character X and submits the form they should get a message saying "you are not allowed to enter special character X".
4 Answers
You can setup field/property validations with the module Field validation.
Here's the documentation on how to add validation to properties (with the Field validation module), as the title isn't a field in Drupal 7.
-
-
I dont understand? Do you want the node title to be a select with a dropdown? Aug 15, 2014 at 7:38
-
-
1That seems like a complete different question to me, maybe update your question or write a new one. You asked about validating the node title field which is a completely different question :) Aug 15, 2014 at 7:43
-
i am just add some validation or any other way user cannot enter special character in node title or they cannot save the node content– AdiAug 15, 2014 at 7:46
Check these module Node Title Validation
These is very light weight module, and helps to validate node titles with min/max characters, Blacklist characters/words and unique node titles.
You can implement hook_node_validate() and check the special character in node title.
function MYMODULE_node_validate($node, $form, $form_state){
// special character list, Add more special character if you want.
$special_character_list = '[\'^£$%&*()}{@#~?><>,|=_+¬-]';
if(preg_match('/' . $special_character_list . '/', $node->title) !== 0){
form_set_error('title', 'Special character are not allowed in form title.')
}
}
UPDATE :
function MYMODULE_node_validate($node, $form, $form_state){
// special character list, Add more special character if you want, escape the character if required.
$special_character_list = '\'^£$%&*()}{@#~?><>,|=_+¬\-\[\]';
if(preg_match('/[' . $special_character_list . ']/', $node->title) !== 0){
form_set_error('title', t("Special characters !special_character_list are not allowed in title.", array('!special_character_list' => $special_character_list)));
}
}
-
Shouldn't that be
t()
rather thanformat_string()
in the updated version?– AndyAug 15, 2014 at 8:40 -
Yes you are right. It should be
t()
if we want to allow translation.t()
callsformat_string()
internally. Aug 15, 2014 at 9:04 -
thank you @MutantMahesh for this if i am apply on webform title field then what i do– AdiAug 15, 2014 at 9:06
-
@user25762 It will work for webform edit form, because webform is also a node. If you want to use this for webform textfield then you can check this in
hook_form_alter
or use drupal.org/project/webform_validation Aug 15, 2014 at 9:10 -
I had the same problem, field validation module work correctly for another field but when I want to validate title I can't validate it and finally find the Title module ,
Usage
After enabling the module as usual, visit the Manage fields page of the content type, taxonomy vocabulary or "comment type" which you wish to replace fields of and click the replace link. That's all.
after installing this module go to your field management and see "Replace" beside title , with this feature, it convert drupal default title and after converting you can work with field title like another fields. I hope this help you and future guys that have the same problem.