Friday, May 15, 2009

Custom Validators: How to have Custom Validation and Required Field Validation

JavaScript functions will show the error message always in the same label, that will be placed somewhere in the screen, as the user wants and not in the custom validator place.

JavaScript Functions to be called from a DataGrid or RadGrid
function ClientValidateIni(source, arguments) {
if (arguments.Value.length == 0) {
arguments.IsValid = false;
document.getElementById("ctl00_ContentPlaceHolder1_txtValidation").value += 'I';
displayMessage(arguments)
}
else {
document.getElementById("ctl00_ContentPlaceHolder1_txtValidation").value = document.getElementById("ctl00_ContentPlaceHolder1_txtValidation").value.replace('I', '');
displayMessage(arguments)
}
}
function ClientValidateWeight(source, arguments) {
if ((arguments.Value.length == 0) && (document.getElementById("ctl00_ContentPlaceHolder1_txtUsine").value == '600')) {
arguments.IsValid = false;
}
else {
if ((parseInt(arguments.Value) == 0) && (document.getElementById("ctl00_ContentPlaceHolder1_txtUsine").value == '600')) {
arguments.IsValid = false;
}
else
{ arguments.IsValid = IsNumeric(arguments.Value); }
}
if (arguments.IsValid == false) {
document.getElementById("ctl00_ContentPlaceHolder1_txtValidation").value += 'W';
displayMessage(arguments)
}
else {
document.getElementById("ctl00_ContentPlaceHolder1_txtValidation").value = document.getElementById("ctl00_ContentPlaceHolder1_txtValidation").value.replace('W', '');
displayMessage(arguments)
}
}
function ClientValidateQty(source, arguments) {
if ((arguments.Value.length == 0) && (document.getElementById("ctl00_ContentPlaceHolder1_txtUsine").value == '600')) {
arguments.IsValid = false;
}
else {
if ((parseInt(arguments.Value) == 0) && (document.getElementById("ctl00_ContentPlaceHolder1_txtUsine").value == '600')) {
arguments.IsValid = false;
}
else
{ arguments.IsValid = IsNumeric(arguments.Value); }
}
if (arguments.IsValid == false) {
document.getElementById("ctl00_ContentPlaceHolder1_txtValidation").value += 'Q';
displayMessage(arguments)
}
else {
document.getElementById("ctl00_ContentPlaceHolder1_txtValidation").value = document.getElementById("ctl00_ContentPlaceHolder1_txtValidation").value.replace('Q', '');
displayMessage(arguments)
}
}

function ClientValidateTemp(source, arguments) {
arguments.IsValid = IsNumeric(arguments.Value);
if (arguments.IsValid == false) {
document.getElementById("ctl00_ContentPlaceHolder1_txtValidation").value += 'T';
displayMessage(arguments)
}
else {
document.getElementById("ctl00_ContentPlaceHolder1_txtValidation").value = document.getElementById("ctl00_ContentPlaceHolder1_txtValidation").value.replace('T', '');
displayMessage(arguments)
}
}
function IsNumeric(input) {
var RE = /^-{0,1}\d*\.{0,1}\d+$/;
return (RE.test(input));
}
function displayMessage(arguments) {
document.getElementById("ctl00_ContentPlaceHolder1_lblErrorGrid1").innerHTML = '';
if (document.getElementById("ctl00_ContentPlaceHolder1_txtValidation").value.indexOf('I') >= 0)
{ document.getElementById("ctl00_ContentPlaceHolder1_lblErrorGrid1").innerHTML = document.getElementById("ctl00_ContentPlaceHolder1_cvtInitial").value + '
'; }
if (document.getElementById("ctl00_ContentPlaceHolder1_txtValidation").value.indexOf('W') >= 0) {
document.getElementById("ctl00_ContentPlaceHolder1_lblErrorGrid1").innerHTML = document.getElementById("ctl00_ContentPlaceHolder1_cvtWeight").value + '
';
}
if (document.getElementById("ctl00_ContentPlaceHolder1_txtValidation").value.indexOf('Q') >= 0)
{ document.getElementById("ctl00_ContentPlaceHolder1_lblErrorGrid1").innerHTML = document.getElementById("ctl00_ContentPlaceHolder1_cvtQty").value + '
'; }
if (document.getElementById("ctl00_ContentPlaceHolder1_txtValidation").value.indexOf('T') >= 0)
{ document.getElementById("ctl00_ContentPlaceHolder1_lblErrorGrid1").innerHTML = document.getElementById("ctl00_ContentPlaceHolder1_cvtTemp").value + '
'; }
}


ASPX - HTML portion code


The output message label:



The Custom Validator and the ControltoValidate (portion of a Grid)


' MaxLength="5">
ClientValidationFunction="ClientValidateTemp"
ControlToValidate="txtTemp" ValidateEmptyText="True" SetFocusOnError="True"
CssClass="error2" Display="Dynamic" ErrorMessage="*">



The hidden controls needed for the example:











No comments:

Post a Comment