Last active
March 16, 2018 15:39
-
-
Save celleb/8401822 to your computer and use it in GitHub Desktop.
Additional methods for jquery.validate.js plugin. Adds functionality to match to input elements ie. Password and confirm password.
Look up functionality - to lookup if the entered info is in the database i.e codes etc. Duplicates - to lookup for duplicates in the database. You need the jquery validation plugin from http://jqueryvalidation.org/
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
(function() { | |
/* This method is to match two input elements */ | |
jQuery.validator.addMethod("match", function(value, element, options) { | |
if (value === $(options).val()) { | |
return true; | |
} else { | |
return false; | |
} | |
}, "Elements do not match"); | |
/* This methods looksup an element in a database or another source */ | |
jQuery.validator.addMethod("lookup", function(value, element, options) { | |
var url = options.pro + '?table=' + options.table + '&field=' + options.field + '&action=lookup&value=' + value; | |
feedback = $.ajax({ | |
url: url, | |
async: false, | |
dataType: 'json', | |
}).responseText; | |
if (feedback == 'true') { | |
return true; | |
} else { | |
return false | |
} | |
}, "Invalid input"); | |
/* This methods looks for duplicates in a database or another source */ | |
jQuery.validator.addMethod("duplicate", function(value, element, options) { | |
var url = options.pro + '?table=' + options.table + '&field=' + options.field + '&action=duplicate&value=' + value; | |
feedback = $.ajax({ | |
url: url, | |
async: false, | |
dataType: 'json', | |
}).responseText; | |
if (feedback == 'true') { | |
return true; | |
} else { | |
return false | |
} | |
}, "Record already exist"); | |
}); |
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
<?php | |
/** | |
* @author Jonas Tomanga <[email protected]> | |
* @copyright (c) 2013, Jonas Tomanga | |
* Description: Process ajax calls | |
*/ | |
/* autoloading classes */ | |
function my_autoloader($class) { | |
include 'classes/' . $class . '.php'; | |
} | |
spl_autoload_register(strtolower('my_autoloader')); | |
@session_start(); | |
$action = trim($_GET['action']); | |
if (isset($action)) { | |
switch ($action) { | |
case 'lookup': | |
$table = trim(strip_tags($_GET['table'])); | |
$field = trim(strip_tags($_GET['field'])); | |
$value = trim(strip_tags($_GET['value'])); | |
$feedback = validate::lookup($table, $field, $value); | |
print json_encode($feedback); | |
break; | |
case 'duplicate': | |
$table = trim(strip_tags($_GET['table'])); | |
$field = trim(strip_tags($_GET['field'])); | |
$value = trim(strip_tags($_GET['value'])); | |
$feedback = validate::duplicate($table, $field, $value); | |
print json_encode($feedback); | |
break; | |
} | |
} |
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
<html> | |
<head> | |
<script type='javascript' src='http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js'> | |
</script> | |
<script type='javascript' src='aajquery.validate.additional-methods.js'> | |
</script> | |
</head> | |
<body> | |
<form name='user' id='user' action='user.php' method='post'> | |
<fieldset> | |
<label class='prefix' for='name'>Name:</label> | |
<input type='text' name='name' id='name' required='required' placeholder='Name'/> | |
<label class='prefix' for='mobile'>Mobile:</label> | |
<input type='tel' name='mobile' id='mobile' required='required' placeholder='XXXXXXXXXXX'/> | |
<label class='prefix' for='pass'>Password:</label> | |
<input type='password' name='pass' id='pass' required='required' placeholder='Password'/> | |
<label class='prefix' for='confirm-pass'>Re-enter password:</label> | |
<input type='password' name='confirm-pass' id='confirm-pass' required='required' placeholder='Re-enter password'/> | |
<label for='code'>Code:</label> | |
<input name='code' id='code' type='text' placeholder='Enter member code' required='required' /> | |
<input type='submit' value='Register' class='small secondary button'/> | |
<input type='reset' value='Reset' class='small secondary button'/> | |
</fieldset> | |
</form> | |
</body> | |
</html> |
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
$("#form form#member").validate({ | |
errorClass: "error", | |
errorElement: "small", | |
debug: true, | |
submitHandler: function(form) { | |
form.submit(); | |
}, | |
rules: { | |
'confirm-pass': { | |
required: true, | |
match: "#pass" //the input element id whose value to match with | |
}, | |
'mobile': { | |
required: true, | |
duplicate: {field: 'mobile', table: 'members', pro: 'ajax.php'} | |
}, | |
'code': { | |
required: true, | |
lookup: {field: 'code', table: 'coupons', pro: 'ajax.php'} | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you want to send data that comes from other input? for example:
data{ id: function(){ return $('#id').val(); }}
I try it on my onw code but on data id i get funcition not value of #id.