javascript - Jquery Validation check username with remote -
i using validation plugin , laravel 4.2. try check username via ajax, can't work. username can taken, doesn't matter if exists or not.
my second issue if using validation rule remote, can't submit form.
does know doing wrong? because in chrome log fine, request sent , can see debug messages.
php
<?php use app\models\user; class validationcontroller extends basecontroller { public function getcheckusername() { if(request::ajax()) { $user = user::where('username', input::get('username'))->get(); if($user->count()) { return response::json(array('msg' => 'true')); } return response::json(array('msg' => 'false')); } } }
js
$(document).ready(function() { $("form").validate({ rules: { username: { required: true, rangelength:[3,255], remote: { url:"http://"+location.host+"/validation/checkusername", type: "get", success: function(msg){ if(msg.msg == 'true') { console.log('exists'); return true; } console.log('doesnt exists'); return false; } }, }, messages: { username: { required: "please enter username!", remote: "username in use!" } } }); });
i tried create own rule via $.validator.addmethod(), doens't work either.
it doesn't sending data. try this. note post request. believe datafilter method should return 'true'
.
update realized logic in php sending true if user exists, therefore logic in javascript should so:
remote: { url: "http://"+location.host+"/validation/checkusername", type: "post", data: { username: function () { return $("input[name='username']").val(); } }, datafilter: function (data) { var json = json.parse(data); if (json.msg == "true") { return "\"" + "that username taken" + "\""; } else { return 'true'; } } }
Comments
Post a Comment