Created
November 28, 2018 18:07
-
-
Save MohamedLamineAllal/bd75b9f4701d35050ee9a1e6cf6f260b to your computer and use it in GitHub Desktop.
Jquery validation bolerplate (with remote, and message overidding, and different examples)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
jquery validation support remote verification (return true or false) (you can use that to verify uniqueness for example | |
*/ | |
$(function() { | |
$("#form-register").validate({ | |
rules: { | |
fname: { | |
required: true | |
}, | |
lname: { | |
required: true | |
}, | |
pass: { | |
required: true, | |
minlength: 10 | |
}, | |
pass_confirm: { | |
required: true, | |
minlength: 10, | |
equalTo: "#pass" | |
}, | |
cname: { | |
required: true, | |
remote: { // <------------------------------------------------------here a remote validation (in this example to check for uniquness otherwise it can be anything) | |
url: "{{ route('data.unique') }}", | |
type: "post", | |
data: { | |
_token: "{{ csrf_token() }}" | |
} | |
} | |
}, | |
email: { | |
required: true, | |
remote: { | |
url: "{{ route('data.unique') }}", | |
type: "post", | |
data: { | |
_token: "{{ csrf_token() }}" | |
} | |
} | |
} | |
}, | |
messages: { // <---------- here how to overide the default messages (for different validation types) | |
fname: { | |
required: "@lang('auth.validation.first_name_required')" | |
}, | |
lname: { | |
required: "@lang('auth.validation.last_name_required')" | |
}, | |
pass: { | |
required: "@lang('auth.validation.password_required')", | |
minlength: "@lang('auth.validation.password_min_len_10')" | |
}, | |
pass_confirm: { | |
required: "@lang('auth.validation.password_confirm_required')", | |
minlength: "@lang('auth.validation.password_min_len_10')", | |
equalTo: "@lang('auth.validation.password_confirm_non_match')" | |
}, | |
cname: { | |
required: "@lang('auth.validation.company_name_required')", | |
remote: "@lang('auth.validation.company_name_exist')" | |
}, | |
email: { | |
required: "@lang('auth.validation.email_required')", | |
remote: "@lang('auth.validation.email_exist')", | |
email: "@lang('auth.validation.email_non_valid')" | |
} | |
} | |
}); | |
}); | |
/* | |
Remote : in jquery validation | |
email: { | |
required: true, | |
remote: { | |
url: "{{ route('data.unique') }}", | |
type: "post", | |
data: { | |
_token: "{{ csrf_token() }}", | |
} | |
} | |
}, | |
*/ | |
// server side [here php laravel] (the most important thing is that your return true or flase in the end; | |
public function checkDataUnicity(Request $request){ | |
$toCheck = $this->extractValueToCheck($request->post()); | |
$isUnique = true; | |
switch ($toCheck) { | |
case 'cname': | |
$company = $request->post()[$toCheck]; | |
$isUnique = Company::isUnique($company); | |
break; | |
case 'email': | |
$email = $request->post()[$toCheck]; | |
$isUnique = User::isUnique($email); | |
// $isValidEmail = | |
break; | |
default: | |
# code... | |
break; | |
} | |
echo $isUnique ? 'true' : 'false'; // here we are echo true or false (in the response) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment