Code: Select all
function LessThanValidator(event) {
var lowID = event.data.lowID;
var highID = event.data.highID;
var formID = event.data.formID;
var lowValue = parseInt($(lowID).val());
//var lowValue = parseInt( $(lowID).attr("value") ); // this doesn't fix
if (isNaN(lowValue)) {
lowValue = 0;
}
$(highID).rules('remove', 'min');
$(highID).rules('add', {
min: lowValue
});
$(formID).validate().element(highID);
return false;
}
function LargerThanValidator(event) {
var lowID = event.data.lowID;
var highID = event.data.highID;
var formID = event.data.formID;
var highValue = parseInt($(highID).val());
//var highValue = parseInt( $(highID).attr("value") ); // this doesn't fix
if (isNaN(highValue)) {
highValue = 0;
}
$(lowID).rules('remove', 'max');
$(lowID).rules('add', {
max: highValue
});
$(formID).validate().element(lowID);
return false;
}
$(document).ready(function() {
// validate the #regFormBody form when it is submitted
$("#regFormBody").validate({
debug: true,
errorPlacement: function(error, element) {
error.insertAfter(element.parent());
},
rules: {
confeelow: {
required: true,
digits: true,
maxlength: 10,
max: isNaN(parseInt($("#confeehigh"))) ? 0 : parseInt($("#confeehigh"))
},
confeehigh: {
required: true,
digits: true,
maxlength: 10,
min: isNaN(parseInt($("#confeelow"))) ? 0 : parseInt($("#confeelow"))
}
},
messages: {
confeelow: {
max: "Please enter a value less than or equal to To (US$)"
},
confeehigh: {
min: "Please enter a value greater than or equal to From (US$)"
}
}
});
///////////////////////////////////////////////////////////////
$("#confeelow").bind("change", {
lowID: "#confeelow",
highID: "#confeehigh",
formID: "#regFormBody"
}, LessThanValidator);
$("#confeehigh").bind("change", {
lowID: "#confeelow",
highID: "#confeehigh",
formID: "#regFormBody"
}, LargerThanValidator);
});Code: Select all
.error {
color: red;
}Code: Select all
Test
Desired Consulting Fee From (US$) *
To (US$) *
Grundsätzlich verwende ich die oben genannten Funktionen, um sicherzustellen, dass der Wert von lowID immer kleiner oder gleich highID ist.
Ich verwende das folgende Skript, um die Validierung einzurichten.
Code: Select all
$("#confeelow").bind("change", { lowID: "#confeelow", highID: "#confeehigh", formID: "#profileFormBody" }, LessThanValidator);
$("#confeehigh").bind("change", { lowID: "#confeelow", highID: "#confeehigh", formID: "#profileFormBody" }, LargerThanValidator);
Hier das HTML-Skript, das die Probleme verursacht:
Code: Select all
Ohne vordefinierten Wert habe ich keine Probleme:
Code: Select all
Einige Leute haben möglicherweise diese Frage an mich: Warum müssen Sie einen Standardwert verwenden, wenn der Code mit neu eingegebenen Daten funktioniert? Hier ist der Grund
- Erstens darf der Benutzer Daten eingeben
- Zweitens sendet der Benutzer das Formular und speichert es in der Datenbank
- Wenn sich der Benutzer das nächste Mal erneut anmeldet, muss ich das Textfeld mit einem Code wie folgt neu füllen:
Code: Select all
Mobile version