Dear Drupal community,

I created a CCK text field and I want users to be able to enter in it only a-z, 0-9 and "-". No uppercase, no spaces, not an underscore, no symbols...

How do I do this? is there a setting in the CCK text field module? is there another module that is doing this?

Thanks a lot in advance for you help,

Yaniv

Comments

WorldFallz’s picture

Maybe the http://drupal.org/project/validation_api module? Otherwise some custom code with hook_field or using the formsapi will probably be required.

Yanivs’s picture

Thanks WorldFallz, Validation API did the trick.

For other people that are looking for this functionality:

  1. Download the http://drupal.org/project/validation_api module and install it
  2. Create a new validator by going to Administer › Site building › Validation AP › Validators › Add. In order to validate lowercase letters, numbers and hyphen, select Regular Expression as type and in the Rule text area put "/^[a-z0-9-]{1,}$/" without the quotes. In the "Default Message" text area put the message you want the user to see when he enters wrong information. Click "Save Validator".
  3. Link the field you want to validate with the validator you just created. You can do this by enabling "Validator link" by going to Administer › Site building › Validation AP › Settings. This will add a link beneath fields all over Drupal. Go to the page that contains the field you want to validate and click on the link beneath it. You will be taken to the "add new field" screen, the "Form ID" and "Field Name" will be already filled in, and you just have to select your validator in the "Validator" drop-down list. Click "Next Step" complete the extra parameters and then click "Save Field"

That's all. In order to be sure that everything is working OK, go to the page that contains the field you want to validate and test the validation by submitting information that should not pass and after that information that should pass.

I hope this will be helpful to other Drupllers out there.

Yaniv

WorldFallz’s picture

awesome-- thanks for posting back the details of your solution!