Important Update: Some Community URL Redirects are Under Maintenance. 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>

1 ACCEPTED SOLUTION

Accepted Solutions

grv.mishra
Collaborator III

<script>
Sys.Application.add_load(function () {

var overallStatus = $("span:contains('Overall Status:')").parent().next().find('li')[0].innerText.trim();
if(overallStatus == "In Draft")
{
$('#master_btnSave').clone().attr('id', 'master_customBtnSave').insertBefore('#master_btnSave');
$('#master_btnSave').hide();
$('#master_customBtnSave').unbind('click').prop("onclick", null).click(function(){ CheckIP('save');return false;});

$('#master_btnApply').clone().attr('id', 'master_customBtnApply').insertBefore('#master_btnApply');
$('#master_btnApply').hide();
$('#master_customBtnApply').unbind('click').prop("onclick", null).click(function(){ CheckIP('apply');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(action)
{
if(!($("span:contains('Machine(s) / System(s):')").parent().next()[1]))
{

if (action == 'save')
{
$('#master_btnSave').click();
}
else if (action == 'apply')
{
$('#master_btnApply').click();
}
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;
}

if (action == 'save')
{
$('#master_btnSave').click();
}
else if (action == 'apply')
{
$('#master_btnApply').click();
}
}

</script>

View solution in original post

21 REPLIES 21

Ilya_Khen
Champion III

Instead of:

$('#master_btnSave').removeAttr('onclick').click(function() { CheckIP(); return false; });
$('#master_btnApply').removeAttr('onclick').click(function() { CheckIP(); return false; });‍‍

 

use:

$('#master_btnSave').clone().attr('id', 'master_customBtnSave').insertBefore('#master_btnSave');
$('#master_btnSave').hide();
$('#master_customBtnSave').unbind('click').prop("onclick", null).click(function(){ CheckIP();return false;});


$('#master_btnApply').clone().attr('id', 'master_customBtnApply').insertBefore('#master_btnApply');
$('#master_btnApply').hide();
$('#master_customBtnApply').unbind('click').prop("onclick", null).click(function(){ CheckIP();return false;});‍‍‍‍‍‍‍‍

 

Also, you never initiate standard Save or Apply procedure, so thus, you have no error, because there is no such command defined in your code.

 

You need to call inside of your CheckIP() function, $('#master_btnSave').click() or $('#master_btnApply').click()

Basically, something like this:

<script>
Sys.Application.add_load(function () {
$('#master_btnSave').clone().attr('id', 'master_customBtnSave').insertBefore('#master_btnSave');
$('#master_btnSave').hide();
$('#master_customBtnSave').unbind('click').prop("onclick", null).click(function(){ CheckIP('save');return false;});


$('#master_btnApply').clone().attr('id', 'master_customBtnApply').insertBefore('#master_btnApply');
$('#master_btnApply').hide();
$('#master_customBtnApply').unbind('click').prop("onclick", null).click(function(){ CheckIP('apply');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(action) {

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[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;
}
}

if (action == 'save') {
$('#master_btnSave').click();
} else if (action == 'apply') {
$('#master_btnApply').click();
}
}
</script>

jsol5
Advocate II

You know there's an IP Address field, right?

pastedImage_1.png

grv.mishra
Collaborator III

Can I hook up save click events based on a field value? I need to do this validation only when record is in draft state.

You need to put condition in the check function or inside event handler. You cannot rewrite handler based on condition, because it is done during the page load.

Well, you can, but that would not be practical at all.

grv.mishra
Collaborator III

Yes I know but there are two more formats allowed on the filed in addition to ip address. Subnet and fqdn, that's why we need to use custom object. Thought my function is named as checkIP but it's doing more than checking IP.

What about this 

If(condition)

{

$('#master_btnSave').clone().attr('id', 'master_customBtnSave').insertBefore('#master_btnSave');
          $('#master_btnSave').hide();
          $('#master_customBtnSave').unbind('click').prop("onclick", null).click(function(){ CheckIP('save');return false;});


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

}

In if condition I will read field value using jQuery selector. And if that value is equal to a particular string I will hook up click events. My only doubt is if dom is ready at that point to use selector to read the value.