Onchange client script validation for String variable

Mrman
Tera Guru

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 .

 

1 ACCEPTED SOLUTION

@ramamr 

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

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

View solution in original post

10 REPLIES 10

Hi Ram,

I have tried the same expression in my code and it is not accepting the string you have added. As shown in below screenshot.

find_real_file.png

Below is my code in client script. Which is checking the expression in format XXXXXXX XXX-X XX. Please modify the code little in the regular expression and then it should work fine.

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

    //Type appropriate comment here, and begin script below
    var regx = /([A-Z]{7})+([\s]{1})+([A-Z]{3})+((-[A-Z]){1})+([\s]{1})+([A-Z]{2})/;
    if (g_form.getValue('short_description').match(regx)) {
        alert('mathcing');

    } else {
        alert('Not matching');
        g_form.clearValue('short_description');
    }
}
Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)