Can we restrict spaces to be enter in a field?

iamlearner
Mega Contributor

Can we restrict spaces to be enter in a field?

Regards,

Lrr

6 REPLIES 6

veena_kvkk88
Mega Guru

Hi Lrr,



Yes you can with the use of regular expressions. You could write an onChange client script on the field in question. (Untested code ahead)


  1. function onChange(control, oldValue, newValue, isLoading, isTemplate) {  
  2.   if (isLoading || newValue === '') {  
  3.   return;  
  4.   }  
  5.   var regex = new RegExp("^[a-zA-Z0-9]*$");  
  6.   if(!regex.test(newValue)){  
  7.   alert('Incorrect field value. Please don't enter spaces');  
  8.   g_form.setValue('ur_field_here','');  
  9.  
  10.   }  
  11. }

This will only allow lower and upper case letters and numbers. You could add special characters too in the regex if you want to allow them as well.


Thanks veena.


It is working.


But the problem is 'It is not allowing the description with spaces'.



Thanks,


Lrr


Use trim() function to remove spaces.


trim(newValue)


How can we make the field read only when it contains the data?


Mark Correct if it solved your issue or hit Like and Helpful if you find my response worthy.


I used before insert/update Business rule with the below.



(function executeRule(current, previous /*null when async*/) {



  var des = current.description.trim();



  if(des == '')


  {


  gs.addErrorMessage("Description can not be null");


  current.setAbortAction(true);


  }



})(current, previous);


Now it is working



Thanks,


Y Anjaneyulu