I have a situation in which I am asking users to enter a site name into a text field with the following rules:

  • no spaces
  • only lowercase letters, numbers, underscores, and dashes

I would like to validate their input and provide feedback if they don't follow the rules. I believe that the "Field Validation" module for Drupal would provide this service. Just checking to see if there is any option for this in Backdrop CMS core or contrib?



Ah, I was pretty sure I started down this path before. 

Some work has been put into porting the field_validation module for Backdrop CMS. 

But, it's not yet stabile enough to be used or for a release. 

I even opened an issue a few months back:

I'll try to find some time to work on this module, but would appreciate any help. 

I'm a little hesitant to share this code, because I'm still testing it. But, this seems to be meeting my basic need right now (for anyone interested). I expect, I'll need to tweak the RegEx validation over time. 

It would still be really nice to get the field validation module working...

function module_name_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'sites_node_form'){
    $form['#validate'][] = 'module_name_custom_validate';

 * Custom sites node form validation

function module_name_custom_validate(&$form, &$form_state) {
  $site_name = $form_state['input']['field_sites_site_name']['und'][0]['value'];
  if (preg_match('/^[a-z0-9\-]{1,31}$/', $site_name) == false) {
    form_set_error('field_sites_site_name',t('You may only use lowercase letters, numbers and dashes in your site name. No spaces allowed. Maximum 30 characters.') );