Programmer man
Programmer man

Reputation: 393

Zend elements or Validator

Is there a zend form element or validator method I can use for user input that accepts both numbers and characters e.g 525555CPG . Or am I going to have to create a custom one.

Upvotes: 1

Views: 88

Answers (4)

t j
t j

Reputation: 7294

If you do not care about validating an exact format, specifically 6 numbers followed by 3 letters, you can use Zend_Validate_Alnum. This gives you a little bit more flexibility than using a regex validator but may not be strict enough for your needs.

$code = '525555CPG';

$validator = new Zend_Validate_Alnum();

if ($validator->isValid($code)) {
    echo $code . ' is a valid code';
} else {
    echo $code . ' is not a valid code';
}

Upvotes: 0

konradwww
konradwww

Reputation: 691

Have you triedZend_Validate_Alnum? I think its the best way.

Upvotes: 1

doydoy44
doydoy44

Reputation: 5772

If your value must be 6-digit followed by three uppercase letters, you can use regular expressions like this:

$test_pattern = "/^([0-9]{6})([A-Z]{3})$/";
$test_validator = new Zend_Validate_Regex(array('pattern' => $test_pattern));
$test_validator->setMessage("The value must be like '999999AAA'");

$test = new Zend_Form_Element_Text('test');
$test->addValidator($test_validator);

For the javascript part, you can use masks like this for example :Masked Input Plugin.

In the demo, there is an example of 'Product Key' which could help you. :)

Upvotes: 2

Mubo
Mubo

Reputation: 1070

If you are Zend_Form class, you can use this.

$myAlphanumericField = new Zend_Form_Element_Text('myfield',
                          array('placeholder' => 'Only letters and numbers'));
$myAlphanumericField        ->setLabel('My Numeric Field')
                                        ->setRequired(true)
                                        ->addFilter('StripTags')
                                        ->addValidator('alnum')
                                        ->getValidator('alnum')->setMessage(' Only letters and numbers are allowed');

Hope that will work for you

Upvotes: 1

Related Questions