1

Currently Im developing a website with Zend framework.

I need help with my suburb name validation

I need the validation to check if the input is in the following format

Suburb Name comma symbol space State abbreviations (State abbreviations consist exact three letters)

Example: MANLY, NSW or SYDNEY, NSW or PALM BEACH, QLD

Basically I need to validate in this format? So how can i do it in Zend framework? Do I need to write custom Zend form validation?

Thanks so much in advance :D

Charles
  • 50,943
  • 13
  • 104
  • 142
user648198
  • 2,004
  • 4
  • 19
  • 26
  • 3
    But what if a user will make a mistake when writing a state? Wouldn't it be better to put state as a select field? – Marcin Mar 10 '11 at 10:43

1 Answers1

1

You can use Zend_Validate_Regex for it. Example:

$myValidator = new Zend_Validate_Regex('/^([a-zA-Z]+),\s(NSW|ACT|VIC|QLD|SA|WA|TAS|NT)$/');
$myValidator->setMessage('Your validation message goes here', Zend_Validate_Regex::NOT_MATCH);

Then add validator to your field.

ischenkodv
  • 4,205
  • 2
  • 26
  • 34
  • To ensure that the state value is exactly three letters, couldn't the pattern be: `'/^([a-zA-Z]+), ([a-zA-Z]{3})$/'`. But @Marcin right: the state field is better as a select control. – David Weinraub Mar 11 '11 at 05:34
  • @David Weinraub It is true about regex. As to select field, I would rather add field description in this case - $field->setDescription('Enter suburb and state in the following format ...etc.'); – ischenkodv Mar 11 '11 at 11:00
  • Actually, I'm not sure that all the [Australian states](http://en.wikipedia.org/wiki/Postcodes_in_Australia#Australia_States_and_territories) use three-letter codes. If OP really doesn't want to use the select control (which I still think is best), then you could improve the regex as follows: `/^([a-zA-Z]+),\s*(NSW|ACT|VIC|QLD|SA|WA|TAS|NT)$/`, couldn't you? – David Weinraub Mar 11 '11 at 16:15