- Node management script
- Easy recipe for posting events with signups on your website
- Handling PDO:_construct error on XAMPP
- Merit badges and counselors for Boy Scout site
- Easy Recipe for a photo gallery with a slideshow block
- How much should a website cost?
- From "Brochureware" to "Communityware"
- rgrep
- Limiting number of characters allowed in a profile field
- Why build websites using Drupal?
Limiting number of characters allowed in a profile field
I finally solved the problem I'd been working on the last week or so: what's the Drupal way to limit the length of a profile field?
Problem: don't let users enter more than 4 characters into a field. (E.g., last four of your social, or last four of your passport, etc.)
Google wasn't too helpful. I only learned from Google that I wanted to set the maxlength attribute on the text input field:
<input type='text' maxlength='4'>
How do I do that in Drupal? I downloaded and enabled the devel modules, including theme developer. When I did an "Inspect Element" on the field I was interested in limiting to 4 characters, I learned that its ID was 'edit-profile-last4'.
After checking the "Themer Info" checkbox in the lower left corner, I clicked on the field I wanted to edit, and learned the following:
function called: theme_textfield(). I clicked on that theme_textfield() text and it opened api.drupal.org on that function.
The themer info textbox also suggested that I could subclass off of the theme_textfield() function via newflash_textfield or phptemplate_textfield. I chose the latter.
So, then I inserted into my template.php file the following function:
function phptemplate_textfield($element) {
# In the event that the textfield is the passport field, set its size to 4.
if ($element['#id'] == 'edit-profile-last4') {
$element['#maxlength'] = 4;
}
return theme_textfield($element);
}
After flushing caches, the field now only allows up to 4 characters!
Hooray!
