Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Fields validation RegEx onChange client script

Mrman
Tera Guru

Hi All,

I have requirement to validate the number entered in a field . For this I created Onchange client script and below RegEx is not working .It is not accepting the number even I give in below format . 

Please guide. 

Number format should be - 123-45-6789

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


    var pattern =  /^([0-9]{3})+([-]{1})+((0-9)]{2})+([-]{1})+([0-9]{4})$/;  

     

    if (!pattern.test(newValue)) {
		g_form.clearValue('u_social_security_usa');
        g_form.showFieldMsg('u_social_security_usa', 'Enter a valid SSN ', 'error', true);
        
    } 
    //Type appropriate comment here, and begin script below

}
1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi

Your RegEx isn't doing quite what you're expecting it to. 

Try with this: 

/^\d{3}-\d{2}-\d{4}$/

 

Cheers
Mike

View solution in original post

3 REPLIES 3

Community Alums
Not applicable

Hi

Your RegEx isn't doing quite what you're expecting it to. 

Try with this: 

/^\d{3}-\d{2}-\d{4}$/

 

Cheers
Mike

Thanks @Mike Reading this worked . 

Could you please let me what was the mistake in my original RegEx .

Does \d indicate a digit ?

Community Alums
Not applicable

Hi @ramamr it was because you'd put a ( rather than a [ in the 2 digit section. 

Yes, \d means digit.

If you want a good visual reputation of what your regex is doing, you can paste it into https://regexr.com/ and it'll tell you down at the bottom what it's trying to validate against.