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.

Validate alpha numeric.

SD29
Tera Expert

Hi All,

I have a requirement where I have to validate a single line text field to have alpha-numeric characters. I have written this onChange script as below, but I think it's not working as expected.

please help me.

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue == '') {

          return;

    }

  g_form.hideFieldMsg('SAP Customer Number');

  var patt = "^[a-zA-Z0-9]*$";

  if (!patt.test(newValue))

  g_form.showFieldMsg('SAP Customer Number','Invalid input','error');

  var patt2 = "^[Nn]/[Aa]$";

  if (!patt.test(newValue))

  g_form.showFieldMsg('SAP Customer Number','Invalid input','error');

   

}

Thanks in Advance,

SD

1 ACCEPTED SOLUTION

I understood the requirement. This time though, I'll repeat the earlier statement and show you the code.




you dont need a regeexp for that. Use .length property of newValue to check.



translates to



var patt = new RegExp("^[a-zA-Z0-9]*$");



if(!patt.test(newValue) || newValue.length != "10"){


        // error out.


}


View solution in original post

5 REPLIES 5

adiddigi
Tera Guru

You are missing constructor.



You should do :


var patt = new RegExp("^[a-zA-Z0-9]*$");



test() is not a method of String class. It's a method of RegExp class.



Also you can just use   var rege = new RegExp("^[a-zA-Z0-9_]*$");   rege.test(newValue) to test for alpha numeric.


Hello Abhiram,



Thanks for your response. This works perfectly. But I need this Alpha-numeric to be 10 Characters only.


How can I do that??



Thanks,


SD


you dont need a regeexp for that. Use .length property of newValue to check.



Mark this as answered if its working.


Hi Abhiram,



I don't think you understand the requirement.



I need the alpha numeric to be 10 Characters only.   Not less than 10 and not more than 10.



Could you please help me here with the script.



Thanks,


SD