Validate single line text variable on catalog item

Gowtham29
Tera Expert

Hi Team, I have single line text variable on the form. User have to enter only numbers starting with 00 always, if they enter alphanumeric values or alphabets then we should not allow user to submit request. Fox Example, if they enter 00asdf34 on the variable then request should not submit, if they 'akfjkjf', then request should not submit, if they enter 753839, then request should not submit, if they enter 001234, then user should need access to submit request. Please help me to validate this variable. Thank you.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

So only numbers are allowed and that too starting with 00 followed by only digits

You can use variable validation regex and apply that on your string variable and no need of any client script etc

Regex - ^[00]{2}[0-9]*$

1) Create variable validation regex from left nav

find_real_file.png

2) then attach this to string variable under type specifications

find_real_file.png

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Nasreenfathima
Giga Expert

Hi Gowtham,

   Please refer this link,

 https://community.servicenow.com/community?id=community_question&sys_id=eb0e96a81bf72010a17c62c4bd4bcb6b

 

Please mark as helpful or correct, if this solution helps you.

Mahesh Kumar3
Giga Guru
Giga Guru

Hi Gowtham,

 

You can write an onChange client script on that text field with the following code:

 

var number = g_form.getValue('your field Name'); // put your variable name here
if(number.startsWith("00")){
  var splitVal = number.split("00");
  var shouldNumeric = splitVal[1];
  if(isNaN(parseInt(shouldNumeric))){
    alert("Number must contain numbers only after 00");
    g_form.setValue('number',''); // put phone nnumber field name correctly here
  }
}else{
  alert("Number must start with 00");
  g_form.setValue('number',''); // put phone nnumber field name correctly here
}

 

Regards,

Mahesh Kumar

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

So only numbers are allowed and that too starting with 00 followed by only digits

You can use variable validation regex and apply that on your string variable and no need of any client script etc

Regex - ^[00]{2}[0-9]*$

1) Create variable validation regex from left nav

find_real_file.png

2) then attach this to string variable under type specifications

find_real_file.png

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Aman Singh_
Kilo Sage

Hi,

 

Onchange client script to validate the input should only be number

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
 var pattern = /^[0-9.]*$/;


  if(!pattern.test(newValue)){  


          alert('Enter only Number');  


          g_form.setValue('field_name', '');   


  }  
   //Type appropriate comment here, and begin script below
   
}

onsubmit client script so that it should not be able to submit until it has '00' in the first place

function onSubmit() {
   //Type appropriate comment here, and begin script below
   var val=g_form.getValue('number');
	if(val.indexOf('00') > -1)
		{
     return true;

} else
	{
g_form.showFieldMsg('number', "It Should start with 00", 'error');
return false;
}
}


Please mark Correct/helpful???? if applicable, thanks!!

Aman