How validate a field using Regular expressions ?

Satya8
Mega Expert

I have field called folder. where i have to allow only

x:\folder or

x:\\folder or

x:\folder\folder\ or

x:\\folder\folder or

x:\\folder\\folder etc etc 

Can i anyone help me with onchange client scipt for this ? I Have tried below code, but getting accurate result..

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

var regExp1 = /\\[a-z]+\\[a-z]+$/i;
var isValid1 = regExp1.test(newValue);
if(!isValid1) {
g_form.addErrorMessage("Enter proper path");
}

}

1 ACCEPTED SOLUTION

Try this instead

/^([a-zA-Z]:)*\\+(\w+\\+)*\w*$/.test(yourString);

ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

View solution in original post

6 REPLIES 6

sachin_namjoshi
Kilo Patron
Kilo Patron

Please use below reg ex in your code

 

^[a-zA-Z]:\\(\w+\\)*\w*$

Then in your client script, you could check if a string matches by

/^[a-zA-Z]:\\(\w+\\)*\w*$/.test(yourString)



Regards,
Sachin

its working for only this value x:\folder

But not for this 

x:\\folder or

x:\folder\folder\ or

x:\\folder\folder or

x:\\folder\\folder or

folder\\folder or

\\folder\folder

Try this instead

/^([a-zA-Z]:)*\\+(\w+\\+)*\w*$/.test(yourString);

ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Has this solved your issue?


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022