Beginners Learn PHP Ask a text box to verify that the problem is not empty

Source: Internet
Author: User
Tags php framework codeigniter
Beginners learn PHP to ask a text box to verify that the problem is not empty?
I am 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 empty! ',
' Last ' = true,
' Required ' = true,
' AllowEmpty ' = False
)
),
' City_name_eng ' = Array (
' Notempty ' = Array (
' Rule ' = ' notempty ',
' Message ' = ' English name cannot be empty! ',
' Last ' = true,
' Required ' = True
)
),
' City_code ' = Array (
' Notempty ' = Array (
' Rule ' = ' notempty ',
' Message ' = ' City code cannot be empty! ',
' Last ' = true,
' Required ' = True
)
)
);
}
?>

It can tell that the text box is not empty, but does not prompt the message out!
The result of my request is that, using the form of MVC (the message mechanism built into the PHP framework), when I enter the text box, if it is empty, I immediately prompt a dialog box (the content you entered is not empty).
Please teach me a detailed process, it is best to add code, for example, a field operation is OK. Thank you

------Solution--------------------
Look, your model should be written by cakephp, right?
The way you do this is not going to satisfy your requirements. This is a validation method that cakephp comes with. This way, validation is done after the submission. But the advantage of this approach is that if you're on the page, if you define the label as "Model.field", The error is automatically output below the input box.

At your request, you can use jquery for verification. You can define the cursor to lose focus events and prompt for error messages. You can find a website member to register, see other people's wording.
------Solution--------------------
In the page

Input (' User.username ', Array (' class ' = ' sinput ', ' onblur ' = ' checkusername (this.value) ', ' maxlength ' =>50, ' Size ' =>30));? >
the user name is 4-16 characters (including Chinese, alphabetic, numeric


JS can write
function Checkusername () {
var username = $ (' #MemberUsername '). Val ();
var target = $ ("#checkusername");
if (username.length <= 3) {
Target.addclass (' error_msg ');
target.html ("User name 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 ("User name consists of letters, numbers, Chinese)";
return false;
} else{
Check that the user name is registered
$.post ('/members/checkusername ', {username:username},function (data) {
if (data== "true") {
Target.addclass (' error_msg ');
target.html ("User name already exists");
return false;
} else{
Target.removeclass (' error_msg ');
Target.html ("  ");
}
})
return true;
}
}
}

------Solution--------------------
discuss

In the page

Input (' User.username ', Array (' class ' = ' sinput ', ' onblur ' = ' checkusername (this.value) ', ' maxlength ' =>50, ' Size ' =>30));? >
User name by 4-16 characters ...

------Solution--------------------
Recommended with CodeIgniter ... With validation class
Http://codeigniter.org.cn/user_guide/libraries/form_validation.html#individualerrors
------Solution--------------------
When I enter the text box, if it is empty, I immediately prompt a dialog box (the content you entered is not empty).

Your state has been decided, you do not have JS (client), PHP (server) is no way to know the state.
Do you think that the client's event is not communicated to the server by certain means of communication, the server can 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.