Text box should start with '_'

mohammedhyderal
Kilo Contributor

Hi All,

I have a requirement of placing a validation   in the text box,

It should start with '_A' , If not it should throw as error

16 REPLIES 16

santoshsahoonis
Kilo Guru

so, when do you want the error to be flashed? On Submit? on when the user types in the box?



1. OnSubmit:



You can check the value and if it doesnt start with '_' , aleart and return false;



2. On the Fly:



You have to add a OnLoad client script and use events like onKeyPress, etc to find if the user is typing the text box with '_' etc.


Tryit Editor v3.1


Ankur Bawiskar
Tera Patron
Tera Patron

Hi Mohammed,



You need to use regular expressions for this. Following is the code snippet which works fine for me. You can use the same. First test this in background script


It checks if string starts with '_A' followed by any character/number any number of times.



The regular expression ^_A.*$ is explained as below:


  • ^ means match at the beginning of the line
  • . followed by * means match any character (.), any number of times (*)
  • $ means to the end of the line


Try by changing the sample string variable in the match() function to validate the output i.e. first use first variable then second etc and check the output



var sampleString1 = '_A1@ADFASasd4'; // match


var sampleString2 = '_Avdds'; // match


var sampleString3 = '_BA'; // Not match


var sampleString4 = '_A'; // match



var reg = new SNC.Regex('/^_A.*$/');


var match = reg.match(sampleString);


if(match != null){


gs.print('match');


}else {


gs.print('Not match');


}



If you need to validate that the string starts with '_A' and at least some character after that and not only '_A'. Use '+' instead of '*'


Regular expression would be ^_A.+$ -> this checks whether there is at least a character/number/special character after '_A'



When this works for you in background script you can place this script in Client Callable Script Include. Call the Script Include function using Synchronous Ajax from client script and thrown an alert based on validation



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


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

Hi Ankur,



I want this to work on the catalog item,



I will explain u in brief,



I have a text box placed in the catalog item,The user needs to type '-a' first followed by his ID, if the users missed then he needs to get the error message.


Hi Mohammed,



Have a onChange client script for the field. Every time the user changes the field value call the script include function and check as per the regular expression whether the value is valid or invalid.


Script include function will return following


if match then return 'valid';


If not match then return 'invalid';



In the client script check whether the returned value is 'invalid' and if yes then throw an alert message and clear the field value



For implementing the script to define client callable script include and GlideAjax refer below link


http://wiki.servicenow.com/index.php?title=GlideAjax



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


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