Intento recibir dos parametros mediante el plugin addMtehod() de Jquery,pero no lo he conseguido, el primer parametro si me lo lee bien, pero el segundo me dice "Undefined"
El codigo:
Código PHP:
PAGINA "FORM.ASP"
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento sin título</title>
<script src="jquery-1.9.0.js"></script>
<script src="jquery.validate.js"></script>
<script src="validate_miForm.js"></script>
</head>
<body>
<form name="miForm" id="miForm" method="post" action="step2.asp">
<div class="error" style="display:none;">
<img src="images/warning.gif" alt="Warning!" width="24" height="24" style="float:left; margin: -5px 10px 0px 0px; " />
<span></span>.<br clear="all"/>
</div>
<table>
<tr><td><label>Unidades por paquete</label></td><td><input type="text" name="val_1" id="val_1" class="required" /></td></tr>
<tr><td><label>Cantidad T</label></td><td><input type="text" name="val_2" id="val_2" class="required" math="#val_1,val3" /></td></tr>
<tr><td><label>Paquetes</label></td><td><input type="text" name="val_3" id="val_3" value="5" disabled="disabled" /></td></tr>
<tr><td><input type="submit" /></td></tr>
</table>
</form>
</body>
</html>
Código PHP:
"PAGINA validate_miForm.js"
// JavaScript Document
$(document).ready(function(){
jQuery.validator.addMethod("password", function( value, element ) {
var result = this.optional(element) || value.length >= 6 && /d/.test(value) && /[a-z]/i.test(value);
if (!result) {
element.value = "";
var validator = this;
setTimeout(function() {
validator.blockFocusCleanup = true;
element.focus();
validator.blockFocusCleanup = false;
}, 1);
}
return result;
}, "Your password must be at least 6 characters long and contain at least one number and one character.");
// a custom method making the default value for companyurl ("http://") invalid, without displaying the "invalid url" message
jQuery.validator.addMethod("defaultInvalid", function(value, element) {
return value != element.defaultValue;
}, "");
jQuery.validator.addMethod("billingRequired", function(value, element) {
if ($("#bill_to_co").is(":checked"))
return $(element).parents(".subTable").length;
return !this.optional(element);
}, "");
jQuery.validator.addMethod("math", function(value,param1,param2) {
alert(value);
//asi funciona, pero solo me trae el parametro1, el parametro2 me dice "undefined"
alert(jQuery(param1).val());
alert(jQuery(param2).val());
//Lo he intentado así y tampoco
//jQuery.validator.addMethod("math", function(value,params) {
//alert(jQuery(params[0]).val());
//alert(jQuery(params[1]).val());
//return this.optional(element) || value == params[0] + params[1];
}, jQuery.validator.format("Please enter the correct value for {0} + {1}"));
//jQuery.validator.messages.required = "";
$("#miForm").validate({
invalidHandler: function(e, validator) {
var errors = validator.numberOfInvalids();
if (errors) {
var message = errors == 1
? 'You missed 1 field. It has been highlighted below'
: 'You missed ' + errors + ' fields. They have been highlighted below';
$("div.error span").html(message);
$("div.error").show();
} else {
$("div.error").hide();
}
},
onkeyup: false,
submitHandler: function() {
$("div.error").hide();
alert("SUBMIT!!!");
},
messages: {
password2: {
required: " ",
equalTo: "Please enter the same password as above"
},
email: {
required: " ",
email: "Please enter a valid email address, example: [email protected]",
remote: jQuery.validator.format("{0} is already taken, please enter a different address.")
}
},
debug:true
});
});