状态 草稿
Todo check grammar, enhance explanations and layout
官方最后更新时间 2008/08/27 09:23

校验辅助函数

为输入项提供校验方法。不过目前可以校验 email 地址,IP,URL,字母数字以及文本。

方法

email()

'email' 检查 email 地址是否符合格式。它比 valid::email_rfc() 方法更加严格。

  • [string] Email 地址
    $email = 'bill@gates.com';
    if(valid::email($email) == true){
         echo "email 校验通过";
    }else{
         echo "email 校验失败";
    }

返回结果:

email 校验通过

email_domain()

'email_domain' 如果域名是一个有效的 MX 记录则会检验 email 地址的域名部分。

  • [string] Email 地址
    $email = 'bill@gates.com';
    if(valid::email_domain($email) == true){
         echo "email 域名校验通过";
    }else{
         echo "email 域名校验失败";
    }

返回结果:

email 域名校验通过
This function uses http://www.php.net/checkdnsrr which is not implemented on Windows platforms. So if your Kohana installation is running in Windows this function will return true no matter if the domain is valid or not. A solution for this is to write your own checkdnsrr function. You can find an implementation here: http://www.php.net/manual/en/function.checkdnsrr.php#82701

email_rfc()

'email_rfc' validates an emailaddress based on the RFC specifications (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";
    }

It will result in HTML as:

Valid email

url()

url($url) does some simple validation on an URL to find out it if it could be existing.

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

It will result in HTML as:

Valid URL

ip()

ip($ip, $ipv6 = FALSE) validates an IP-address to make sure it could exist, but does not guarantee it actually does.

  • [string] IP-address to be validated
  • [bool] allow IPv6 addresses (default FALSE)
$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";

It will result in HTML as:

Valid IP
Invalid IP

credit_card()

'credit_card' checks if a credit card number is valid or not depending on the configuration defined on your credit_cards.php config file.

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

This method will check if the credit card number is valid taking into account the settings defined in the credits_cards.php config file. If nothing is passed as second parameter then the default type of the credits_cards.php config file will be used.

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

It will result in HTML as:

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";
}

It will result in HTML as:

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";
}

It will result in HTML as:

Valid phone number

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";
}

It will result in HTML as:

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

It will result in HTML as:

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 2 is cool";
if(valid::alpha_numeric($string) == true){
    echo "Valid string";
}else{
    echo "Invalid string";
}

It will result in HTML as:

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

It will result in HTML as:

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";
}

It will result in HTML as:

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

It will result in HTML as:

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";
}

It will result in HTML as:

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

It will result in HTML as:

Valid

numeric()

'numeric' checks whether a string is a valid number (negative and decimal numbers allowed)

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

It will result in HTML as:

Valid

standard_text()

'standard_text' checks whether a string is a valid text. Letters, numbers, whitespace, dashes, periods, and underscores 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";
}

It will result in HTML as:

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";
}

It will result in HTML as:

Valid decimal
 
<code php>

$decimal = '4.5'; $format = array(2,1); if(valid::decimal($decimal,$format) == true){

  echo "Valid decimal";

}else{

  echo "Invalid decimal";

} </code>

It will result in HTML as:

Invalid decimal
helpers/valid.txt · 最后更改: 2008/11/02 04:54 由 icyleaf