1.How to only allow alphabets for a field on a onchange client script? 2.How to allow only alphanumeric characters for a field on onchange client script?

mrafi
Giga Contributor
2 REPLIES 2

michal29
Mega Guru

Hello Mohammed,

 

 

 

You can use regular expression for that: like described here regex - Regular Expression for alphanumeric and underscores - Stack Overflow

 

For service-now I recommend using it in this manner:

 

var field = g.form.getValue('FIELD_YOU_WANT_TO_TEST');
var testREG = /^[a-zA-Z0-9_]*$/;

if (!testREG.test(field)) {
  // HERE IS WHAT HAPPENS IF ENTERED VALUE DOES NOT MATCH
} else {
  // HERE IS WHAT HAPPENS IF ENTERED VALUE DOES MATCH
}

 

Regards,

 

Michal

 

Mujtaba Amin Bh
Mega Guru
  1. You can customize the below code as per your requirement:
  2. function onLoad() {  
  3.   // CHANGE: 'short_description' to your field name  
  4.   g_form.getControl('short_description').observe('keypress',function(evt) {  
  5.       // Browsee black magic voodoo to get the CharCode pressed  
  6.       var charCode = (typeof evt.which == "number") ? evt.which : evt.keyCode;  
  7.  
  8.       // If keyed pressed is the equivalent of 0-9 (charcode 48-57) or a period (charcode 46)  
  9.       if ((evt.keyCode >= 48 && evt.keyCode <= 57) || (evt.keyCode == 46)) {  
  10.       // Do nothing  
  11.       }  
  12.       else {  
  13.       // Suppress keypress  
  14.       evt.preventDefault();  
  15.       }  
  16.   });  
  17. }