Validate for String

Pradeep Jagadal
Kilo Contributor

Hi All,

I am new to servicenow.   I have field on form of type String and I want to validate it for only characters .

Please someone help me to achieve this.

Thanks in advance.

Chinnu

1 ACCEPTED SOLUTION

Pradeep J
Kilo Guru

Hi ,



write onChange() client script on a field as follwo




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


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


          return;


    }




    //Type appropriate comment here, and begin script below


  var name=g_form.getValue('<field name>');



  if (!isNaN(name))


    {


  alert("Only Characters are allowed");


  g_form.setValue('<field name>','');


  return false;


    }


}



Thanks


Pradeep D J


PS - Please mark Helpful, Like, or Correct Answer if applicable.


View solution in original post

4 REPLIES 4

Pradeep J
Kilo Guru

Hi ,



write onChange() client script on a field as follwo




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


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


          return;


    }




    //Type appropriate comment here, and begin script below


  var name=g_form.getValue('<field name>');



  if (!isNaN(name))


    {


  alert("Only Characters are allowed");


  g_form.setValue('<field name>','');


  return false;


    }


}



Thanks


Pradeep D J


PS - Please mark Helpful, Like, or Correct Answer if applicable.


Thank you Pradeep Its working for me.


Chuck Tomasi
Tera Patron

I'll assume by 'characters' you mean letters (no numbers or special symbols.)



You can use an onChange or onSubmit client script and use a regular expression something like this: (NOTE: UNTESTED CODE AHEAD)



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


  if (newValue === '') {


      return;


  }


var s = g_form.getValue('your_field_name');


var pattern = /[^A-Za-z]/g;   // checks


if (s.search(pattern) >= 0) {


        g_form.showFieldMsg('your_field_name', 'Only letters are allowed here', 'error');


        return false;


} else {


        g_form.hideFieldMsg('your_field_name');


        return true;


}



}



Docs: Client Scripts


Docs: GlideForm


Client Script Best Practices - ServiceNow Wiki      


TechNow Episode List (see episode 31)


Getting error message as " s is not a defined function() ";