Form表单验证控件分享

来源:互联网 发布:党员 信仰 知乎 编辑:程序博客网 时间:2024/05/30 05:01

问题:
日常开发过程中,经常碰到这样的问题:
页面需要提交一大堆数据,但是业务逻辑非常简单,只是简单的CRUD。这时候在页面JS代码和后台PHP代码中需要写一大堆表单数据的验证操作。如下:

//创建活动
public function DoDetail() {
dtBeg=strreplace("","",this->get(“dtBeg”));
dtEnd=strreplace("","",this->get(“dtEnd”));
if (!Tools::IsDate(dtBeg,/\d4\d2\d2\d2:\d2:\d2/’)) {
$this->OutputJSON(-1, “请输入正确的开始日期”);
}

if (!Tools::IsDate($dtEnd, '/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/')) {    $this->OutputJSON(-1, "请输入正确的结束日期");}$sName = $this->get("sName");$sName = Text::UTF8toGBK($sName);if (empty($sName)) {    $this->OutputJSON(-5, "请设置活动名称");}$sKeywords = $this->get("sKeywords");$sKeywords = Text::UTF8toGBK($sKeywords);if (empty($sKeywords)) {    $this->OutputJSON(-4, "请设置活动关键字");}$sKeywords = rtrim($sKeywords, ";");$failMsg =$this->get("failMsg");$failMsg = Text::UTF8toGBK($failMsg);if (empty($failMsg)) {    $this->OutputJSON(-2, "请设置礼包领取失败的提示语");}$failMsg = rtrim($failMsg, ";");$reParticipate = $this->get("reParticipate");$reParticipate = Text::UTF8toGBK($reParticipate);if (empty($reParticipate)) {    $this->OutputJSON(-3, "请设置重复中奖的提示语");}$reParticipate = rtrim($reParticipate, ";");$luckyFloor    = $this->get("luckyFloor");$luckyFloorPkt = $this->get("luckyFloorPkt");$luckyFloorPkt = Text::UTF8toGBK($luckyFloorPkt);if (!empty($luckyFloor) && empty($luckyFloorPkt)) {    $this->OutputJSON(-6, "请设置中尾数楼对应的礼包");}$luckyFloor = rtrim($luckyFloor, ";");if(strlen($luckyFloor) > 2000){    $this->OutputJSON(-8, "幸运楼层数太长了。不能超过2000字节");}$specialFloor    = $this->get('specialFloor');$specialFloorPkt = $this->get("specialFloorPkt");$specialFloorPkt = Text::UTF8toGBK($specialFloorPkt);if (!empty($specialFloor) && empty($specialFloorPkt)) {    $this->OutputJSON(-7, "请设置中特殊楼层奖励对应的礼包");}$specialFloor = rtrim($specialFloor, ";");if(strlen($specialFloor) > 2000){    $this->OutputJSON(-8, "特殊楼层数太长了。不能超过2000字节");}....

}
可以看到上面的代码非常的丑陋,大部分的工作都在表单的验证,编码过程非常的消耗时间。但是又不能不写,否则,如果没有前端就会存在SQL注入等问题。

那么我们能否做一些优化呢?

  1. 在PHP方面:
    参考了CI等框架,我移植了一个通用的form验证模块,消除了CI的From_Validtion模块对CI的依赖,使用方法非常简单简洁,并且支持非常多验证类型。详细各种验证方式请参考CI Form_Validation文档:
    https://codeigniter.com/user_guide/libraries/form_validation.html

用法样例:

require_once “Form_validation.php”;

validtion=newCIFormvalidation();validtion->set_data($_GET);//如果是POST数据,则不需要该行

validtion>setrules(username,Username,required);validtion->set_rules(‘password’, ‘Password’, ‘required’,
array(‘required’ => ‘必须设置密码 %s.’));
validtion>setrules(passconf,PasswordConfirmation,required);validtion->set_rules(‘email’, ‘Email’, ‘required’);
validtion>setrules(ip,IP,required|validip);validtion->set_rules("ids", "ID", "required|regex_match[/^\d+(,\d+)*$/]”);

if (validtion->run() == FALSE)  
{  
    echo “Failed”;  
    echo
validtion->error_string();
}
else
{
echo “success!”;
}

….
2. 在前端页面js方面:
同样地,在js方面,也有相应的jQuery插件,如下:

//CSS
.errorTip{
background-image:url(/static/images/access_disallow.gif);
background-repeat:no-repeat;
padding-left:16px;
}

    .errorInput{        background-color:#FFCC33;    }    .validTip{        background-image:url(/static/images/access_allow.gif);        background-repeat:no-repeat;        background-position:left top ;        padding:2px;    }

//form表单上填写相关验证数据

上传

//引用相关Jquery插件:

//js里面简单的使用
$(“#frmImportCDK”).checkForm();
结束语:
积极拥抱开源,学习开源代码,让你提高更快!

0 0
原创粉丝点击