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

dman’s picture

case 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

Officecase’s picture

Thanks for that great site!! I have now confirmed that my expression works. Using this code....

$card=strlen($node->field_dcard[0]['value']);
 if (preg_match('/^[a-zA-Z]{1}[-]{1}[0-9]{4}$/', $card))  {
form_set_error('field_text','The field is in the form of A-0000.');
}

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?

dman’s picture

Yes, 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. :-)

Officecase’s picture

Status: Active » Closed (fixed)

Alrighty.... I figured it out.

$card=($node->field_dcard[0]['value']);
if(preg_match('/^[a-zA-Z]{1}[-]{1}[0-9]{4}$/', $card)==0) {
form_set_error('field_text',"The field is in the form of A-0000.");
}

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!!!!!!