A text box verification is not empty.

Source: Internet
Author: User
Tags php for beginners
For new php students, how can I solve the problem that the text box verification is not empty? On the model layer: & nbsp; & lt ;? PhpclassCityextendsAppModel {& nbsp; var $ name = 'Cities '; var $ validate = php for beginners ask a question about text box verification not empty?
On the model layer:

Class City extends AppModel {
Var $ name = 'Cities ';
Var $ validate = array (
'City _ name' => array (
'Notempty '=> array (
'Rule' => 'notempty ',
'Message' => 'City name cannot be blank! ',
'Last' => true,
'Requestred' => true,
'Allowempty '=> false
)
),
'City _ name_eng '=> array (
'Notempty '=> array (
'Rule' => 'notempty ',
'Message' => 'the English name cannot be blank! ',
'Last' => true,
'Required' => true
)
),
'City _ code' => array (
'Notempty '=> array (
'Rule' => 'notempty ',
'Message' => 'City code cannot be blank! ',
'Last' => true,
'Required' => true
)
)
);
}
?>

It can judge that the text box is not empty, but no message is prompted!
The result I requested is: in the form of mvc (built-in message mechanism in php framework), if it is blank when I enter it in the text box, A dialog box is displayed immediately (the content you entered is not empty ).
Please teach me a detailed process. it is best to add code. for example, the operation of a field is OK. Thank you.

------ Solution --------------------
Should your model be written in cakephp?
You cannot meet your requirements. this is the built-in verification method of cakephp. in this way, verification is performed only after submission. however, the advantage of this method is that if you use "Model. field "to define tags. errors are automatically output under the input box.

As per your requirements, you can use jquery for verification. you can define the cursor focus loss event to prompt the error message in time. you can find a website member to register and check others' statements. this method is basically used.
------ Solution --------------------
In the page

Input ('user. username ', array ('class' => 'sinput', 'onblur' => 'checkusername (this. value) ', 'maxlength' => 50, 'size' => 30);?>
The username consists of 4-16 characters including Chinese characters, letters, and numbers.


JavaScript can be written
Function checkUserName (){
Var username = $ ('# MemberUsername'). val ();
Var target = $ ("# checkusername ");
If (username. length <= 3 ){
Target. addClass ('error _ msg ');
Target.html ("the user name must be less than 3 characters ");
Return false;
} Else {
Var rule =/^ [A-Za-z0-9 \ u4E00-\ u9FA5] + $ /;
If (! Rule. test (username )){
Target. addClass ('error _ msg ');
Target.html ("username consists of letters, numbers, and Chinese characters ");
Return false;
} Else {
// Check whether the user name is registered
$. Post ('/members/checkusername', {username: username}, function (data ){
If (data = "true "){
Target. addClass ('error _ msg ');
Target.html ("the user name already exists ");
Return false;
} Else {
Target. removeClass ('error _ msg ');
Target.html ("");
}
})
Return true;
}
}
}

------ Solution --------------------
Discussion

In the page

Input ('user. username ', array ('class' => 'sinput', 'onblur' => 'checkusername (this. value) ', 'maxlength' => 50, 'size' => 30);?>
The user name consists of 4-16 characters ......

------ Solution --------------------
Codeigniter... is recommended for Configuration Verification
Http://codeigniter.org.cn/user_guide/libraries/form_validation.html#individualerrors
------ Solution --------------------
When I enter it in the text box, if it is empty, a dialog box is immediately displayed (the content you enter is not empty ).

You have decided on this status. if you do not need js (client), php (server) cannot know this status.
Do you think that the client event is not transmitted to the server through certain communication methods, and the server will be able to know the occurrence of this event?

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.