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

@Ankur Bawiskar Could you let me know if below RegEx is fine for pattern to check this condition "Minimum of 4 digits Maximum of 15"

 

var pattern = /^[A-Z]{4,15}$/

Hi,

try to first check if the value is digit and then check length using length property on string and do validation

regards
Ankur

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

@Ankur Bawiskar The pattern it should allow is "NNNNNNN NNN-N AA"

Hi Ram,

try the below regular expression:

var regx = /([A-Z]{7})+([\s]{1})+([A-Z]{3})+((-[A-Z]){1})+([\s]{1})+([A-Z]{2})/gi;

 

Please mark this answer as correct and helpful if it resolves the query and helpful alone if it lead you in right direction.

Thanks,

Mohit Kaushik

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)

@Mohit Kaushik I have tried RegEx you mentioned but the issue it is still allowing if I enter 16 Characters as shown below . 

 

Please suggest . 

GHTY45ty89UJ23ty

Below is my Client script :

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


    var pattern = /([A-Z]{7})+([\s]{1})+([A-Z]{3})+((-[A-Z]){1})+([\s]{1})+([A-Z]{2})/gi;
	var grct = g_form.getValue('u_employment_registration_book_ctps');
	
	var grctlen = grct.toString().length;
	alert("the length is :" + grctlen);

    if (!pattern.match(grct) && grctlen != 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);
		//g_form.hideFieldMsg('u_employment_registration_book_ctps');
    
		//}
	}


    //Type appropriate comment here, and begin script below

}