[stackoverflow] jQuery validation engine

3897131/how-do-i-return-true-and-false-from-another-function-inside-in-jquery 3897697/how-to-write-this-regular-expression

HTML

<script src="http://www.position-relative.net/creation/formValidator/js/jquery.validationEngine.js"></script>
<link rel="stylesheet" href="http://www.position-relative.net/creation/formValidator/css/validationEngine.jquery.css">
<h3>Test form validation on dynamically added fields</h3>
<a href="#">Add Field</a>

<form id="form" action="javascript:alert('form submitted');">
    <b>0:</b><input id="i_0" type="text" class="validate[custom[isOne],funcCall[isValid]]"><br>
    <input type="file" class="validate[required]" />
    <input id="submit" type="submit">
</form>

JavaScript

//Dynamically added fields
var i=0;
$('a').click(function(){
    $('<b>'+(++i)+':</b><input id="i_'+i+'" type="text" class="validate[custom[isOne],funcCall[isValid]]"><br>').insertBefore('#submit');
});


//Validation function
//Problem: we do not have any context information about which input is validated
window.isValidImpl = function(){
    //This is a tricky hack which relies on the exact order of the arguments and function calls...
    //If anything changes in the way the plugin is implemented, 
    //or if the function is called from somewhere else than it will probably break
    var _this = arguments.callee.caller.arguments[0];
    return $(_this).val()=='1';
}


//Setup jQuery validation
$.validationEngineLanguage = $.validationEngineLanguage || {};
$.validationEngineLanguage.allRules = $.extend($.validationEngineLanguage.allRules,{
    //this would go in the lang file
    "isOne":{
        "regex":"/^1$/",
        "alertText":"* Only '1' is valid [regex]"
    },
    "isValid":{
        "nname":"isValidImpl",
        "alertText":"* Only '1' is valid [func]"
    }
});

//Start the validation engine
$('#form').validationEngine({
    validationEventTriggers:"change"
});