Ankur Bawiskar
Tera Patron

@ramamr 

You can use Regular expression for this and attach that Regex to the variable

Regards
Ankur

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

@Ankur Bawiskar  Please see below Client script . It is not working and allowing characters which are not matching Regex and just checking if length is 16 .

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

}

@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  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

@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  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader