提供一款jquery validate 添加唯一性验证实例,有需要的朋友可以参考一下。
//html 添加class onlyEmail
代码如下 | 复制代码 |
<?= $this->formText('user_profile[email]', null, array('class' => 'textfield email onlyEmail')); ?> //zend 输出后如下 <input type="text" class="textfield email onlyEmail" value="" id="user_profile_inv-email" name="user_profile_inv[email]"> //js $.validator.addMethod("onlyEmail", function(value, element) { return eval($.ajax({ url: "/user/ajaxrpc/exsitcheck", type: 'POST', async: false, data: { val:value, name:'email', base:"user_profile" } }).responseText); }, "Email has been exist!"); |
//php 程序处理
代码如下 | 复制代码 |
public function exsitcheckAction() { //init db object $db; $base = $this->_request->getPost('base'); $name = $this->_request->getPost('name'); $data = $this->_request->getPost('val'); if ($base == 'user_profile') { $db = new User_Model_ProfileMapper(); } if ($db) { if ($this->dataNoExist($name, $data, $db)) { echo true; } } echo false; } |