Is there a client script to ensure that a field only contains numbers?

princes
Giga Contributor

I need a client script for a field that should contain an accounting code that should only be numeric.   People sometimes put, "I don't know what this is"....and we want them to find there accounting code for their department.   The field should only accept numbers.   Does anyone have a client script for this?

4 REPLIES 4

vamsisai
Tera Guru

Alikutty A
Tera Sage

Hi,



You can write onChange client script on the field to validate for number.



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


    if (isLoading || newValue === '') {


          return;


    }



  if(isNan(newValue)){


    g_form.setValue('field_name', '');   //This should be your number field name


    g_form.addInfoMessage('You should enter a number');


    }



}



Thanks


Please Hit like, Helpful or Correct depending on the impact of the response


Harish KM
Kilo Patron
Kilo Patron

This should do


function onChange(control, oldValue, newValue, isLoading, isTemplate) {


    if (isLoading || newValue === '') {


          return;


    }


  //check only numbers


var pattern = /[0-9]/;


  if(!pattern.test(newValue)){  


          alert('enter only   number');  


          g_form.setValue('u_account_code', '');   //u_account_code is the field name


  }  


   


}



find_real_file.png


Regards
Harish

The provided code working as expected only.