Validation class of codeigniter class library

Source: Internet
Author: User
Tags valid email address codeigniter

In BS programming, we often need to verify the data submitted by the customer. Each scripting language has its own Authentication mode, and codeignite also provides a set of very useful class libraries-validation class.

Like other class libraries, You need to load them before using the validation class.

$ This-> load-> Library ("validation ");

After loading, we can use $ this-> Validation to reference it. Some may ask, "How does it know what data I want to verify and what rules I want to verify ?" This is what we will talk about below.

The loaded validation class can't do anything. Even if it calls its run method, its return value is always false, because we didn't specify a validation rule for it. Codeigniter predefines some rule references.

Rules

Parameters

Description

Example

Required None Required field  
Matches Yes Matches the specified parameter. Matches [form_item]
Min_length Yes Minimum Length Min_length [6]
Max_length Yes Specify the maximum length Max_length [12]
Exact_length Yes Specify the numerical precision Exact_length [4]
Alpha None The input must be a letter  
Alpha_numeric None The input must be a letter or Digit  
Alpha_dash None The input must be a letter or a lower part or minus sign.  
Numeric None The input must be a numeric character only.  
Valid_email None Enter a valid email address.  
Valid_ip None Enter a valid IP address.  

Assume there is a page for user registration information verification. There is an input box named user, a password input box named password, and a Password confirmation box named confirm on this page, we can define verification rules as follows:

$ Rule ['user'] = "reguired"; $ rule ['Password'] = "required | min_length [6]"; $ rule ['Confirm'] = "required | matches [Password]"; $ this-> Validation-> set_rules ($ rule );

After the verification rules are defined, we can call the run method for data verification.

If ($ this-> Validation-> Run () = false) {// Verification Failed} else {// Verification Successful}

Use$ This-> Validation-> error_stromgYou can get the prompt message after the verification fails.

All of the above are verification rules defined in advance using codeigniter. What should we do if we want to customize verification rules? This is also very simple. We only need to write a verification function and specify it to validation.

// User-defined authentication username function. Do not add "callback _" function username_check ($ Str) {if ($ STR = "admin") to the function name ") {$ this-> Validation-> set_message ("username_check", 'the % s field can not be the word "admin" '); retrun false;} else {true ;}} $ rule ['user'] = "callback_username_check"; // before the function name is verified, add "Callback _" $ this-> Validation-> set_rules ($ Tule );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.