alpha customizable alpha / alphanumeric validator with client validation support

  1. Requirements
  2. Usage
  3. Available additional parameters
  4. accentedLetters list
  5. allAccentedLetters list
  6. Example of extra usage
  7. Disclaimer

alpha is a simple alpha / alphanumeric validator that allows you to customize it as you want to.

Requirements

Yii 1.1.7

Usage

to use alpha inside your model simply store its file into your application extensions directory and then use the rule

/**
 * Declares the validation rules.
 */
public function rules()
{
	return array(
		// name, email, subject and body are required
		array('email, subject, body', 'required'),
		array('name', 'ext.alpha'),
		// email has to be a valid email address
		array('email', 'email'),
	);
}

Available additional parameters

if you won't use any additional parameter alpha will validate any string with just basic alpha characters with a length of at least 1.

Here are the additional parameters you can use, and keep in mind that you can use all of them together:

  • allowNumbers is a boolean, and defaults to false. Allow the use of numbers into the string.
  • allowSpaces is a boolean and defaults to false. the name is pretty self explanatory.
  • minChars is an integer and defaults to 1.
  • maxChars is still an integer and defaults to NULL. If you don't want the validated string to have more then x characters use this parameter.
  • accentedLetters is another boolean. Enables the use of basic accented vowels into the string. See the list below.
  • allAccentedLetters still a boolean. Enable the use of every accented letter existing in the latin-derived languages. See the list below.
  • extra this is an array. If you want to allow some other characters (like -) this is the parameter you want to use.
  • message just like any other validator you can use this to change the error message. You can use {attribute} in this string as a wild card if you want to.

accentedLetters list

ÀÁÂÃÄĀĂÈÉÊËĚĔĒÌÍÎÏĪĨĬÒÓÔÕÖŌÙÚÛÜŪŬŨàáâãäāăèéêëēěĕ
ìíîïīĩĭòóôõöōŏùúûüūŭũ

allAccentedLetters list

ÀÁÂÃÄÅĀĄĂÆÇĆČĈĊĎĐÈÉÊËĒĘĚĔĖĜĞĠĢĤĦÌÍÎÏĪĨĬĮİIJĴĶŁĽĹĻ
ĿÑŃŇŅŊÒÓÔÕÖØŌŐŎŒŔŘŖŚŠŞŜȘŤŢŦȚÙÚÛÜŪŮŰŬŨŲŴÝŶŸŹŽŻàá
âãäåāąăæçćčĉċďđèéêëēęěĕėƒĝğġģĥħìíîïīĩĭįıijĵķĸłľĺ
ļŀñńňņʼnŋòóôõöøōőŏœŕřŗśšşŝșťţŧțùúûüūůűŭũųŵýÿŷžżź
ÞþßſÐð

Example of extra usage

/**
 * Declares the validation rules.
 */
public function rules()
{
	return array(
		// name, email, subject and body are required
		array('email, subject, body', 'required'),
		array('name', 'ext.alpha', 'extra' => array('-', '_')),
		// email has to be a valid email address
		array('email', 'email'),
	);
}

every character you'll use in extra will be escaped, so you shouldn't have to worry about anything.

Disclaimer

Keep in mind that this validation rule is not doing any conversion of the string (no htmlspecialchars, no htmlentities). Be always careful about how to save strings containing accented letters into your database.

8 1
10 followers
1 637 downloads
Yii Version: 1.1
License: BSD-2-Clause
Category: Validation
Developed by: nickcv
Created on: Apr 20, 2011
Last updated: 7 years ago

Downloads

show all