Is there any standard validation class for checking whether the first character in the textfield sis uppercase and alphabet, or one should write the own validator for this purpose, I have read the standard validation docs regarding Alnum and Alpha, but since my requirement is sort of specific, I wanted to get the opinions of the Zend Programmers
Is there any standard validation class for checking whether the first character in the
Share
You can either use the
RegexValidatoror write your own.The advantage in creating your own validator for this special purpose is that a. your code becomes more readable (using a
FirstCharIsUppercaseLettervalidator vs. reading the regex of aRegexValidator) and b. you can pre-define the default validation error message.I’d probably create such a validator using the
AlphaValidatorto validate that the first char is a letter and then check if mb_strtoupper, applied on the first char, equals the original first char.// Edit: For best usability, depending on what you’re trying to do, a filter could also do the job (to ensure the first letter is uppercase). There’s a
StrToUpperfilter that you could easily extend to only change the first char. Thus, in case the user forgets to capitalize his input, it’s done automatically without having to review it.