Valid Helper

Fournit des méthodes de validation des entrées. Il dispose actuellement de validation des adresses email, adresses IP, URL, des chiffres / nombres et du texte.

Cette aide fournit des fonctions de validation à l'aide de la validation library.

Methods

email()

'email' vérifie si une adresse e-mail est valide.

  • Il est plus stricte que les valid::email_rfc() méthode.
  • [string] Email address to validate
    $email = 'bill@gates.com';
    if(valid::email($email) == true){
         echo "Valid email";
    }else{
         echo "Invalid email";
    }

Résultat HTML :

Valid email

email_domain()

'email_domain' valide la partie domaine d'une adresse e-mail en vérifiant si le domaine a un record MX valide.

  • [string] Email address to validate
    $email = 'bill@gates.com';
    if(valid::email_domain($email) == true){
         echo "Valid email domain";
    }else{
         echo "Invalid email domain";
    }

Résultat HTML :

Valid email domain

Cette fonction utilise http://www.php.net/checkdnsrr qui n'est pas mis en œuvre sur les plates-formes Windows. Donc, si votre installation est en cours d'exécution dans Kohana Windows cette fonction retournera vrai, peu importe si le domaine est valide ou non. Une solution pour cela est d'écrire votre propre fonction checkdnsrr. Vous pouvez trouver une mise en œuvre ici: http://www.php.net/manual/en/function.checkdnsrr.php#82701

email_rfc()

'email_rfc' valide une adresse e-mail basé sur les spécifications RFC (http://www.w3.org/Protocols/rfc822/). This validation is less strict than the valid::email() function.

  • [string] Email address to validate
    $email = 'bill@gates.com';
    if(valid::email_rfc($email) == true){
         echo "Valid email";
    }else{
         echo "Invalid email";
    }

Résultat HTML :

Valid email

url()

url($url) fait une simple validation sur une URL pour découvrir que si elle pourrait être en vigueur.

  • [string] URL to be validated
    $url = 'http://www.kohanaphp.com';
    if(valid::url($url) == true){
         echo "Valid URL";
    }else{
         echo "Invalid URL";
    }

Résultat HTML :

Valid URL

ip()

ip($ip, $ipv6 = FALSE, $allow_private = TRUE) valide une adresse IP pour vous assurer qu'elle pourrait existent, mais ne le garantit pas.

  • [string] $ip IP-address to be validated
  • [bool] $ipv6 allow IPv6 addresses (default FALSE)
  • [bool] $allow_private allow private addresses (default TRUE)
$ip="65.181.130.41";
if(valid::ip($ip) == true){
    echo "Valid IP";
}else{
    echo "Invalid IP";
}
 
$ip="123.456.678.912";
if(valid::ip($ip) == true){
    echo "Valid IP";
}else{
    echo "Invalid IP";

Résultat HTML :

Valid IP
Invalid IP

credit_card()

'credit_card' vérifie si un numéro de carte de crédit est valide ou non en fonction de la configuration définie sur votre credit_cards.php fichier de configuration.

  • [string] Card number to be validated
  • [string|array] Card type, or an array of card types – default = NULL

Cette méthode vérifie si le numéro de carte de crédit est valable pour prendre en compte les paramètres définis dans le fichier de configuration credits_cards.php. Si rien n'est passé comme second paramètre, le type par défaut du fichier credits_cards.php sera utilisé.

$number = "4992739871600";
if(valid::credit_card($number)) {
    echo "Valid credit card number";
} else {
    echo "Invalid credit card number";
}

Résultat HTML :

Valid credit card number

This method allows to pass as second argument an array of credit_cards type (which you define in the credit_cards.php config file). If you do so, the number will be considered valid if it matches with the specifications of at least one of the types added in the array.

Consider the following example:

$number = "4992739871600";
$types = array(
    'default',
    'american express'
);
if(valid::credit_card($number, $types)) {
    echo "Valid credit card number";
} else {
    echo "Invalid credit card number";
}

Résultat HTML :

Valid credit card number

Since the credit number passed as first argument matches at least the rules defined for the default type.

phone()

'phone' checks whether a phone number is valid or not. It strips all the characters which are not a digit from the string at the moment of the validation.

  • [string] The phone number to check
  • [array] Optional array containing the allowed lengths – defaults = array(7,10,11)
$phone = '+54 123-456 789';
if(valid::phone($phone) == true){
    echo "Valid phone number";
}else{
    echo "Invalid phone number";
}

Résultat HTML :

Valid phone number

date()

'date' checks whether a string is a valid date string.

  • [string] The date string to check.
$date = '19th February 2009';
if (valid::date($date)) {
	echo "Valid date";
} else {
	echo "Invalid date";
}

Résultat HTML :

Valid date 

alpha()

'alpha' checks whether a string consists of alphabetical characters only

  • [string] String to be validated
  • [boolean] If true UTF-8 mode will be used – default = FALSE
$string="KohanaPHP is cool";
if(valid::alpha($string) == true){
    echo "Valid string";
}else{
    echo "Invalid string";
}

Résultat HTML :

Invalid string
$string="KohanaPHPiscool";
if(valid::alpha($string) == true){
    echo "Valid string";
}else{
    echo "Invalid string";
}

Résultat HTML :

Valid string

alpha_numeric()

'alpha_numeric' checks whether a string consists of alphabetical characters and numbers only

  • [string] String to be validated
  • [boolean] If true UTF-8 mode will be used – default = FALSE
$string="KohanaPHP Version 3 is cool";
if(valid::alpha_numeric($string) == true){
    echo "Valid string";
}else{
    echo "Invalid string";
}

Résultat HTML :

Invalid string
$string="KohanaPHPVersion2iscool";
if(valid::alpha_numeric($string) == true){
    echo "Valid string";
}else{
    echo "Invalid string";
}

Résultat HTML :

Valid string

alpha_dash()

'alpha_dash' checks whether a string consists of alphabetical characters, numbers, underscores and dashes only

  • [string] String to be validated
  • [boolean] If true UTF-8 mode will be used – default = FALSE
$string="KohanaPHP Version 2 is cool";
if(valid::alpha_dash($string) == true){
    echo "Valid string";
}else{
    echo "Invalid string";
}

Résultat HTML :

Invalid string
$string="KohanaPHP_Version-2-is_cool";
if(valid::alpha_dash($string) == true){
    echo "Valid string";
}else{
    echo "Invalid string";
}

Résultat HTML :

Valid string

digit()

'digit' checks whether a string consists of digits only (no dots or dashes)

  • [string] String to be validated
  • [boolean] If true UTF-8 mode will be used – default = FALSE
$digits = "23424.32";
if(valid::digit($digits) == true){
    echo "Valid";
}else{
    echo "Invalid";
}

Résultat HTML :

Invalid
$digits = "2342432";
if(valid::digit($digits) == true){
    echo "Valid";
}else{
    echo "Invalid";
}

Résultat HTML :

Valid

numeric()

'numeric' checks whether a string is a valid number (negative and decimal numbers allowed). It supports international formats (ex. Spanish decimal format uses comma as separator).

  • [string] the input string
$number = "-23424.32";
if(valid::numeric($number) == true){
    echo "Valid";
}else{
    echo "Invalid";
}

Résultat HTML :

Valid

standard_text()

'standard_text' checks whether a string is a valid text. Letters, numbers, whitespace, dashes (-), periods (.), underscores (_) and normal punctuation are allowed.

  • [string] Text to be validated
$text = 'this is not a valid text because of the : character';
if(valid::standard_text($text) == true){
    echo "Valid standard text";
}else{
    echo "Invalid standard text";
}

Résultat HTML :

Invalid standard text

decimal()

'decimal' Checks if a string is a proper decimal format. The format array can be used to specify a decimal length, or a number and decimal length, eg:

 * array(2) would force the number to have 2 decimal places, array(4,2)
 * would force the number to have 4 digits and 2 decimal places.
  • [string] string to be validated
  • [array] decimal format: array(y) or array(x,y) - default NULL
$decimal = '4.5';
if(valid::decimal($decimal) == true){
    echo "Valid decimal";
}else{
    echo "Invalid decimal";
}

Résultat HTML :

Valid decimal
$decimal = '4.5';
$format = array(2,1);
if(valid::decimal($decimal,$format) == true){
    echo "Valid decimal";
}else{
    echo "Invalid decimal";
}

Résultat HTML :

Invalid decimal
helpers/valid.txt · Dernière modification: 20/02/2011 19:31 (modification externe)
CC Attribution-Noncommercial-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0