Important Update: Community URLs redirect issues are partially resolved. Learn More. .

cancel
Showing results for 
Search instead for 
Did you mean: 

Custom object to verify textbox value format on Save and Save and close

grv.mishra
Collaborator III

Hello All,

 

                I have a requirement to validate format of input text field on save or save and close. I want to skip this validation if this filed is black or not in DOM. I wrote below code but this code broke Save and Save and Close functionality also I dont see any error on console. Please suggest how to go about it. Also I need to abort save in case format is not matching, In my script if format is not matching popup is shown but on click of ok save happens which I want to avoid.

 

<script>

Sys.Application.add_load(function() {
$('#master_btnSave').removeAttr('onclick').click(function() { CheckIP(); return false; });
$('#master_btnApply').removeAttr('onclick').click(function() { CheckIP(); return false; });
var timer = null;
var inputControl = $("span:contains('Machine(s) / System(s):')").parent().next().find('input').next();
inputControl.keydown(function(){
clearTimeout(timer);
timer = setTimeout(CheckIP, 2000)
});

});

function CheckIP(){

if(!($("span:contains('Machine(s) / System(s):')").parent().next()[1]))
{
return;
}
var Result=-1;

var ArrSp=$("span:contains('Machine(s) / System(s):')").parent().next()[1].getElementsByTagName("p")[0].innerText.trim();
if(ArrSp.trim() !=""){
var aa=ArrSp.split(",");

for (i = 0; i < aa.length; i++) {
var CheckIP = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
var CheckDomainName=/^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$/;
var Subnet1 = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\-\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
var Subnet2 = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\/\d+$/;
if(aa !="" ){
if(CheckIP.test(aa.trim()) || CheckDomainName.test(aa.trim()) || Subnet1.test(aa.trim()) || Subnet2.test(aa.trim()))
{
}
else
{
Result=i;
}
}
}
if(Result>-1){
WarningAlert(aa[Result]+ " is not fully qualified domain name/IP or Subnet.","Machine(s) / System(s): format warning");
return;
}
}
}
</script>

21 REPLIES 21

<script>
var CheckIPPassed,CheckCloudAccountPassed,CheckCloudSystemPassed,CheckAccountOUPassed;
Sys.Application.add_load(function () {

if($("span:contains('Overall Status:')"))
{
if( typeof ($("span:contains('Overall Status:')").parent().next().find('li')[0]) === "undefined" || $("span:contains('Overall Status:')").parent().next().find('li')[0] == null )
{
return;
}
var overallStatus = $("span:contains('Overall Status:')").parent().next().find('li')[0].innerText.trim();
if(overallStatus == "In Draft")
{
CheckIPPassed = false;
CheckCloudAccountPassed=false;
CheckCloudSystemPassed = false;
CheckAccountOUPassed = false;
$('#master_btnSave1').clone().attr('id', 'master_btnSave1').insertBefore('#master_btnSave1');
$('#master_btnSave1').hide();
$('#master_btnSave1').unbind('click').prop("onclick", null).click(function(){ CheckIP('save');CheckCloudSystem('save');CheckAccountOU('save');CheckCloudAccount('save');return false;});

$('#master_btnApply1').clone().attr('id', 'master_btnApply1').insertBefore('#master_btnApply1');
$('#master_btnApply1').hide();
$('#master_btnApply1').unbind('click').prop("onclick", null).click(function(){ CheckIP('apply');CheckCloudSystem('apply');CheckAccountOU('apply');CheckCloudAccount('apply');return false;});

var timer = null;
var inputCloudAccount,inputCloudSystem;
var inputControl;
var inputAccountOU;

if(typeof($("span:contains('Machine(s) / System(s):')").parent().next().find('input').next()) === "undefined" || $("span:contains('Machine(s) / System(s):')").parent().next().find('input').next() == null)
{
inputControl = null;
}
else
{
inputControl = $("span:contains('Machine(s) / System(s):')").parent().next().find('input').next();
}

if(typeof($("span:contains('Cloud Account ID(s):')").parent().next().find('input').next()) === "undefined" || $("span:contains('Cloud Account ID(s):')").parent().next().find('input').next() == null)
{
inputCloudAccount = null;
}
else
{
inputCloudAccount = $("span:contains('Cloud Account ID(s):')").parent().next().find('input').next();
}

if(typeof($("span:contains('Cloud System(s):')").parent().next().find('input').next()) === "undefined" || $("span:contains('Cloud System(s):')").parent().next().find('input').next() == null)
{
inputCloudSystem = null;
}
else
{
inputCloudSystem = $("span:contains('Cloud System(s):')").parent().next().find('input').next();
}


if(typeof($("span:contains('Account(s)/OU:')").parent().next().find('input').next()) === "undefined" || $("span:contains('Account(s)/OU:')").parent().next().find('input').next() == null)
{
inputAccountOU = null;
}
else
{
inputAccountOU = $("span:contains('Account(s)/OU:')").parent().next().find('input').next();
}

if(inputControl != null)
{
inputControl.keydown(function(){
clearTimeout(timer);
timer = setTimeout(CheckIP, 2000)
});
}

if (inputCloudAccount != null)
{
inputCloudAccount.keydown(function(){
clearTimeout(timer);
timer = setTimeout(CheckCloudAccount, 2000)
});
}

if(inputCloudSystem != null)
{

inputCloudSystem.keydown(function(){
clearTimeout(timer);
timer = setTimeout(CheckCloudSystem, 2000)
});
}

if(inputAccountOU != null)
{
inputAccountOU.keydown(function(){
clearTimeout(timer);
timer = setTimeout(CheckAccountOU, 2000)
});

}

}
}

});

function CheckIP(action)
{
if(!($("span:contains('Machine(s) / System(s):')").parent().next()[1]))
{

if (action == 'save')
{
$('#master_btnSave1').click();
}
else if (action == 'apply')
{
$('#master_btnApply1').click();
}
return;
}
var Result=-1;
var ArrSp = "";

if($("span:contains('Machine(s) / System(s):')").parent().next()[1].getElementsByTagName("p")[0] != undefined)
{
ArrSp=$("span:contains('Machine(s) / System(s):')").parent().next()[1].getElementsByTagName("p")[0].innerText.trim();
}

if(ArrSp.trim() !="")
{
var aa=ArrSp.split(",");

for (i = 0; i < aa.length; i++)
{
var CheckIP = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
var CheckDomainName=/^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$/;
var Subnet1 = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\-\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
var Subnet2 = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\/\d+$/;
if(aa[i] !="" )
{
if(CheckIP.test(aa[i].trim()) || CheckDomainName.test(aa[i].trim()) || Subnet1.test(aa[i].trim()) || Subnet2.test(aa[i].trim()))
{

}
else
{
Result=i;
}
}

}

}

if(Result>-1)
{
WarningAlert(aa[Result]+ " is not fully qualified domain name/IP or Subnet.","Machine(s) / System(s): format warning");
return;
}
else
{
CheckIPPassed = true;
}

}

function CheckCloudAccount(action)
{

if(!($("span:contains('Cloud Account ID(s):')").parent().next()[1]))
{

if (action == 'save')
{
$('#master_btnSave1').click();
}
else if (action == 'apply')
{
$('#master_btnApply1').click();
}
return;
}

var Result=-1;
var ArrCloudAccount = "";
if($("span:contains('Cloud Account ID(s):')").parent().next()[1].getElementsByTagName("p")[0] != undefined)
{
ArrCloudAccount=$("span:contains('Cloud Account ID(s):')").parent().next()[1].getElementsByTagName("p")[0].innerText.trim();
}

if(ArrCloudAccount.trim() !="")
{
var aa=ArrCloudAccount.split(",");

for (i = 0; i < aa.length; i++)
{
var CloudAccountID = /^\d{1,12}$/;
var AzureSystemID =/(\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}(\}){0,1}$/;

if(aa[i] !="" )
{
if(CloudAccountID.test(aa[i].trim()) || AzureSystemID.test(aa[i].trim()))
{

}
else
{
Result=i;
}
}

}

}

if(Result>-1)
{
WarningAlert(aa[Result]+ " is not fully qualified Cloud Account ID(s).","Cloud Account ID(s): format warning");
return;
}

else
{
CheckCloudAccountPassed = true;

}
if(CheckCloudAccountPassed && CheckIPPassed && CheckCloudSystemPassed && CheckAccountOUPassed)
{
if (action == 'save')
{
$('#master_btnSave1').click();
}
else if (action == 'apply')
{
$('#master_btnApply1').click();
}
}
}


function CheckCloudSystem(action)
{
if(!($("span:contains('Cloud System(s):')").parent().next()[1]))
{

if (action == 'save')
{
$('#master_btnSave1').click();
}
else if (action == 'apply')
{
$('#master_btnApply1').click();
}
return;
}

var Result=-1;
var ArrCloudSystem = "";
if($("span:contains('Cloud Account ID(s):')").parent().next()[1].getElementsByTagName("p")[0] != undefined)
{
ArrCloudSystem=$("span:contains('Cloud System(s):')").parent().next()[1].getElementsByTagName("p")[0].innerText.trim();
}

if(ArrCloudSystem.trim() !="")
{
var aa=ArrCloudSystem.split(",");

for (i = 0; i < aa.length; i++)
{
var SystemAccount = /^[a-zA-Z][a-zA-Z0-9]{1,25}$/;
var SystemAccountFQDN = /^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$/;
var AWSSystems = /^i-[a-zA-Z0-9]{1,25}$/

if(aa[i] !="" )
{
if(SystemAccount.test(aa[i].trim()) || SystemAccountFQDN.test(aa[i].trim()) || AWSSystems.test(aa[i].trim()) )
{

}
else
{
Result=i;
}
}

}

}

if(Result>-1)
{
WarningAlert(aa[Result]+ " is not fully qualified Cloud System(s).","Cloud System(s): format warning");
return;
}
else
{
CheckCloudSystemPassed = true;
}


}

function CheckAccountOU(action)
{

if(!($("span:contains('Account(s)/OU:')").parent().next()[1]))
{

if (action == 'save')
{
$('#master_btnSave1').click();
}
else if (action == 'apply')
{
$('#master_btnApply1').click();
}
return;
}

var Result=-1;
var ArrAccountOU = "";
if($("span:contains('Account(s)/OU:')").parent().next()[1].getElementsByTagName("p")[0] != undefined)
{
ArrAccountOU=$("span:contains('Account(s)/OU:')").parent().next()[1].getElementsByTagName("p")[0].innerText.trim();
}

if(ArrAccountOU.trim() !="")
{
var aa=ArrAccountOU.split(",");

for (i = 0; i < aa.length; i++)
{
var IndividualAccountFormat = /^[a-zA-z]{0,3}\\[a-zA-Z0-9_]{1,25}$/;
var OU = /^OU=[a-zA-Z0-9_\\\- ]{1,50}$/;
var DC = /^DC=[a-zA-Z0-9_\\\- ]{1,50}$/;

if(aa[i] !="" )
{
if(IndividualAccountFormat.test(aa[i].trim()) || OU.test(aa[i].trim()) || DC.test(aa[i].trim()) )
{

}
else
{
Result=i;
}
}

}

}

if(Result>-1)
{
WarningAlert(aa[Result]+ " is not fully qualified Account(s)/OU:.","CAccount(s)/OU: format warning");
return;
}
else
{
CheckAccountOUPassed = true;

}


}

</script>

Ilya_Khen
Champion III

Yes, quite a lot to validate

1. You are cloning the master_btnSave1 / master_btnApply1 buttons and then name the cloned buttons again as master_btnSave1 / master_btnApply1. Which makes cloning basically redundant and not working.

2. It is very hard to validate without your fields, form, etc. So you need to debug the exceptions in the browser console by using DevTools. It will give you some hints on where the issue happened.