【No.5】全角文字数チェック – Zend Validateの派生クラスを公開します

By nakayama - 09/02/27 - このエントリをはてなブックマークに追加このエントリをYahoo!ブックマークに追加このエントリをdel.icio.usに追加このエントリをFC2ブックマークに追加

「携帯サイト制作には欠かせないデータ入力チェック『Zend Validate』の派生クラスを公開します」の5つ目として全角文字数の形式をチェックするクラスを公開します。

Zend ValidateにはStringLengthのチェックがあるのですがバイト数で数え、全角文字列だと2文字扱いになってしまいます。
全角文字を1文字として数えるのがこのクラスの特徴となります。

全角文字数チェック


require_once 'Zend/Validate/Abstract.php';

class My_Validate_MbStringLength extends Zend_Validate_Abstract
{

    const TOO_SHORT = 'stringLengthTooShort';
    const TOO_LONG  = 'stringLengthTooLong';

    /**
     * @var array
     */
    protected $_messageTemplates = array(
        self::TOO_SHORT => '%sを全角%d文字以上で入力して下さい',
        self::TOO_LONG  => '%sを全角%d文字以内で入力して下さい',
    );

    /**
     * @var array
     */
    protected $_messageVariables = array(
        'min' => '_min',
        'max' => '_max'
    );

    /**
     * Minimum length
     *
     * @var integer
     */
    protected $_min;

    /**
     * Maximum length
     *
     * If null, there is no maximum length
     *
     * @var integer|null
     */
    protected $_max;

    /**
     * Sets validator options
     *
     * @param  integer $min
     * @param  integer $max
     * @return void
     */
    public function __construct($min = 0, $max = null)
    {
        $this->setMin($min);
        $this->setMax($max);
    }

    /**
     * Returns the min option
     *
     * @return integer
     */
    public function getMin()
    {
        return $this->_min;
    }

    /**
     * Sets the min option
     *
     * @param  integer $min
     * @return Zend_Validate_StringLength Provides a fluent interface
     */
    public function setMin($min)
    {
        $this->_min = max(0, (integer) $min);
        return $this;
    }

    /**
     * Returns the max option
     *
     * @return integer|null
     */
    public function getMax()
    {
        return $this->_max;
    }

    /**
     * Sets the max option
     *
     * @param  integer|null $max
     * @return Zend_Validate_StringLength Provides a fluent interface
     */
    public function setMax($max)
    {
        if (null === $max) {
            $this->_max = null;
        } else {
            $this->_max = (integer) $max;
        }

        return $this;
    }

    /**
     * Defined by Zend_Validate_Interface
     *
     * Returns true if and only if the string length of $value is at least the min option and
     * no greater than the max option (when the max option is not null).
     *
     * @param  string $value
     * @return boolean
     */
    public function isValid($value)
    {
        $valueString = (string) $value;
        $this->_setValue($valueString);
        $length = mb_strlen($valueString);
        if ($length < $this->_min) {
            $this->_error(self::TOO_SHORT);
        }
        if (null !== $this->_max && $this->_max < $length) {
            $this->_error(self::TOO_LONG);
        }
        if (count($this->_messages)) {
            return false;
        } else {
            return true;
        }
    }

}

その他Zend Validate派生クラス

【No.1】. ひらがなチェック

データにひらがな以外の文字があるかチェックを行ないます。

【No.2】.メールアドレスチェック

携帯電話で使われているメールアドレスをチェックを行ないます。

【No.3】. 電話番号チェック

電話番号の形式になっているかチェックします。

【No.4】. 郵便番号チェック

郵便番号の形式になっているかチェックします。

【No.5】.全角文字数チェック

全角文字数の最小、最大数をチェックします。

【No.6】.全角カタカナチェック

データに全角カタカナ以外の文字があるかチェックを行ないます。

【No.7】.厳密な未入力チェック

『0』と『null』を区別した入力チェックを行ないます

【No.8】.URLチェック

URLが形式に則っているかチェックします。