Hi,

I'm busy with Drupal 7 (awesome!) and I'm tinkering with content types. I'd like to create a content type field that accepts dutch license plates, which are in the form of XX-XX-XX or XX-XXX-X.
X is always uppercase alphanumeric.

How can I validate if it's posted in either format?

Thanks,

Dennis

Comments

Jaypan’s picture

Look at the field API. It allows you to create new fields with their own validation.

FLX’s picture

Thank you for your swift reply, it's much appreciated :-)
I'm very new at Drupal 7 and the field API isn't fully explanatory, would you happen to have an example for me to chew on?

Many thanks,

Dennis

Jaypan’s picture

Here's a start: http://drupal.org/node/707832

Unfortunately it's not a great start. I read about it in a book. But I'm sure if you do some searching around you can find more information in order to do this.

bartl’s picture

I have the same problem. I googled and found your post. And next, I found this post, which describes a new module: Field validation.

It solves our problem rather neatly without tinkering with PHP code.

What it doesn't do (one for the wishlist...) is cleaning up data so it conforms more to an ideal, for example, changing lowercase letters into uppercase, trimming whitespace, etc.

lang14’s picture

You could create a custom template for the content type that performs those operations, or better yet use CSS to do that for you.

.uppercase {
text-transform:uppercase;
}

manu.joseph’s picture

Thankz bartl.. That was the exact module i was looking for to validate additional profile fields that i have added in user registration pages for Drupal 7...

Tankeroo’s picture

Can this module verify the field based on a list of pre-determined words/characters?

I'd like to add a custom field to user profile for user to input a serial number, which is matched to a predetermined list of serial numbers. Can this or another mod do something like that?

Thanks

BlackyWhoElse’s picture

Hey ho heres a small example:

<?php

// this hole code is done in this editor here so you should check it on your own (vars and so on)
function mymodule_form() {

$form['serial'] = array(
  '#type' => 'fieldset',
  '#title' => t('Enter Serial Number'),
  '#weight' => 1,
  '#collapsible' => TRUE,
  '#collapsed' => TRUE,
);
$form['serial']['number'] = array(
  '#type' => 'textfield',
  '#title' => t('Product name xyz'),
  '#size' => 60,
  '#maxlength' => 128,
  '#required' => TRUE,
);

}

function mymodule_form_validate($form,$form_state) {

$key = $form['values']['serial']['number'];

if(!empty($key)){
        if (check_this_key($key) == FALSE) {
            form_set_error('', t('Serial is invaild'));
        }
 }
}

function check_this_key($key){
 //check it here 
}

?>
Tankeroo’s picture

Hey BlackyWhoElse, Thanks for that code. I know it's been a while, I didn't get a notification of a reply. >.<
I'll try to test it and give feedback as soon as I can (which may take a while).