- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-08-2020 03:39 PM
Hi All,
I am trying to validate a String variable to check it has only Letters and does not contain numbers and it should be in below format having 13 letters
Could you help me with the Onchange Script for alpha numeric , I tried below but not working .
Also I am not sure how to validate the format I mentioned above .Please suggest .
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-09-2020 05:47 AM
try either of the RegEx as per your requirement
Also no need of checking the length; the regular expression will handle this
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// this will allow lower and upper case
var pattern = /^([A-Za-z]{7})+([\s]{1})+([A-Za-z]{3})+((-[A-Za-z]){1})+([\s]{1})+([A-Za-z]{2})$/;
// OR
// this will only allow upper case
//var pattern = /^([A-Z]{7})+([\s]{1})+([A-Z]{3})+((-[A-Z]){1})+([\s]{1})+([A-Z]{2})$/;
if (!pattern.test(newValue)){
g_form.clearValue('u_employment_registration_book_ctps');
g_form.showFieldMsg('u_employment_registration_book_ctps', 'Enter a valid CTPS ID ', 'error', true);
}
//Type appropriate comment here, and begin script below
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-08-2020 04:43 PM
Hi you could check your format with soemthing like this
if(myText.length == 16 && myText.charAt(7) == ' ' && myText.charAt(11) == '-' && myText.charAt(13) == ' ' ) {
g_form.addInfoMessage("we have a winner');
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-09-2020 05:04 AM
You can use Regular expression for this and attach that Regex to the variable
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-09-2020 05:11 AM
Please suggest .
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var pattern = /^[A-Z-\s]/;
//alert("the length is :" + newValue.length);
if ((!pattern.test(newValue)) && (newValue.length != 16)){
//if(newValue.length != 16){
g_form.clearValue('u_employment_registration_book_ctps');
g_form.showFieldMsg('u_employment_registration_book_ctps', 'Enter a valid CTPS ID ', 'error', true);
//}
}
//Type appropriate comment here, and begin script below
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-09-2020 05:47 AM
try either of the RegEx as per your requirement
Also no need of checking the length; the regular expression will handle this
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// this will allow lower and upper case
var pattern = /^([A-Za-z]{7})+([\s]{1})+([A-Za-z]{3})+((-[A-Za-z]){1})+([\s]{1})+([A-Za-z]{2})$/;
// OR
// this will only allow upper case
//var pattern = /^([A-Z]{7})+([\s]{1})+([A-Z]{3})+((-[A-Z]){1})+([\s]{1})+([A-Z]{2})$/;
if (!pattern.test(newValue)){
g_form.clearValue('u_employment_registration_book_ctps');
g_form.showFieldMsg('u_employment_registration_book_ctps', 'Enter a valid CTPS ID ', 'error', true);
}
//Type appropriate comment here, and begin script below
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader