How to prevent submission of form on onchange client script

KARAN24
Tera Contributor

Hi Team,

We have a on change client script and script include to fetch the value from table and do validation,but the issue is even if the value is incorrect the form is getting submitted,which should not be the case. Below is script include and client script.

Script Include-

var getTax = Class.create();
getTax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    getCampus: function () {
        var taxid = this.getParameter('sysparm_u_party_tax_id');
        
        
        var tax1 = new GlideRecord('customer_account');
        tax1.addEncodedQuery('u_party_tax_id!=NULL');
        tax1.query();
        var arr_final=[];
    while(tax1.next())
    {
        var arr=tax1.u_party_tax_id.split(',');
        
        for(var i=0;i<arr.length;i++)
        {
            if(arr[i]==taxid)
            {
                arr_final.push(1);
            }
        }
        
    }
   return arr_final.length;
        
      
            
        
    
    },
    

                                        
    
    

    isPublic:function(){return true;},
    type: 'getTax'
});
 
2.On change client script-
function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
var user_id = g_form.getValue('u_party_tax_id');
    var a=user_id.toUpperCase();
    
    var ga = new GlideAjax('sn_customerservice.getTax');
    ga.addParam('sysparm_name''getCampus');
    ga.addParam('sysparm_u_party_tax_id',a);
    ga.getXML(NameDetails);
    function NameDetails(response){
        
        var answer = response.responseXML.documentElement.getAttribute("answer");
/
        
         if (answer >= 1) {
           
             
             g_form.setValue('u_party_tax_id',a);
            
        } 
        else 
        {
            
            
            g_form.clearValue('u_party_tax_id');
            g_form.showFieldMsg('u_party_tax_id''Please enter Valid Tax ID','error',true);
            
            
            
        }
        
     
    
    }
}

 


 
    


      
    

    

    
   //Type appropriate comment here, and begin script below
   
4 REPLIES 4

Aman Kumar S
Kilo Patron

Why would onChange be used for validating the submission, better use onsubmit client script or before BR if you want to validate something before submission.

Best Regards
Aman Kumar

Hi Aman,

How can I use the validation of same field on onsubmit client script.

I used the below script for ONSubmit

var user_id = g_form.getValue('u_party_tax_id');
    var a=user_id.toUpperCase();
    //alert(a);
    //g_form.addInfoMessage("Timer:" +user_id);
    var ga = new GlideAjax('sn_customerservice.getTax');
    ga.addParam('sysparm_name''getCampus');
    ga.addParam('sysparm_u_party_tax_id',a);
    ga.getXML(NameDetails);
    function NameDetails(response){
        //alert("testalert");
        var answer = response.responseXML.documentElement.getAttribute("answer");
//alert(answer);
        
         if (answer >= 1) {
           // g_form.addInfoMessage("Verified");
             g_form.setValue('u_test',a);
             
            
        } 
        else 
        {
            //g_form.addErrorMessage("Not Verified");
           return false;
            
            
            
        }
        
     
    
    }
}

 

Thanks,

Karan

For onsubmit, you can't use Async ajax, as the form will not be held until the response from server comes back. Try changing this to getXMLWait, then it will work as expected which makes Sync ajax call

https://docs.servicenow.com/bundle/utah-api-reference/page/app-store/dev_portal/API_reference/GlideA...

Best Regards
Aman Kumar

Hi Amam,
Can we use anything except getXMLWait because this function won't work in the scoped application.