Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I am trying to understand how to put together my Validation Code:
This what I am trying.
$card=strlen($node->field_dcard[0]['value']);
if (preg_match('/^[a-z]{1}-[0-9]{4}$/', $card)){
form_set_error('field_text','The field is in the form of A-0000.');
}
No matter what I type in the field I cant get a result.
Comments
Comment #1
dman CreditAttribution: dman commentedcase sensitive? Your message is wrong, it'll match 'a-0000' but not 'A-0000'.
Either make the match insensitive, or strtoupper() it before matching and change [a-z] to [A-Z].
Or whatever, depending what you are doing.
http://www.quanetic.com/regex.php
Comment #2
Officecase CreditAttribution: Officecase commentedThanks for that great site!! I have now confirmed that my expression works. Using this code....
If I enter a value that is outside of this expression, like a-12345, it should return a no match. But I do not receive the form_set_error().
Am I missing something?
Comment #3
dman CreditAttribution: dman commentedYes, I was thinking to mention it after I posted ... but went back to work instead ...
It's probably in the way you use
form_set_error(). Does that field really have no parents? Inspect the view-source on the page to see if the field has an ID that may give you a clue.
... it may also be because the both the codes you paste set an error when the entry is actually valid. :-)
Comment #4
Officecase CreditAttribution: Officecase commentedAlrighty.... I figured it out.
I was using 'strlen' which was giving me the count of characters in that field. Once I figured that out it was all down hill. Thanks for your help and Thanks for a great module!!!!!!