Validate alpha numeric on a field in catalog item

SD29
Tera Expert

Hi Folks,

I am trying to validate an alpha-numeric field of length 10 in a catalog item. It's giving me an error even though I have used the alpha-numeric. I am doing this by onChange client script.

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

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

          return;

    }

  g_form.hideFieldMsg('sap_customer_number');

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

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

  g_form.showFieldMsg('sap_customer_number','Invalid input','error');

}

Here are the results when I used this script:

Screen Shot 2017-03-03 at 12.02.31 PM.png

Thanks in Advance,

SD

1 ACCEPTED SOLUTION

That's strange, since it worked in my developer instance.   Here's the full script:



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


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


            return;


  }


 


  g_form.hideFieldMsg('sap_customer_number');


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


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


            g_form.showFieldMsg('sap_customer_number','Invalid input','error');


}




The only other thing I did was force newValue to a string in line 8.


View solution in original post

12 REPLIES 12

That's strange, since it worked in my developer instance.   Here's the full script:



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


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


            return;


  }


 


  g_form.hideFieldMsg('sap_customer_number');


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


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


            g_form.showFieldMsg('sap_customer_number','Invalid input','error');


}




The only other thing I did was force newValue to a string in line 8.


Hi Fred,



Is there any possibility that we can get this on OnSubmit() Client script?



I wrote the following code but it is still submitting.



function onSubmit() {


    //Type appropriate comment here, and begin script below


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


  var projectKey=g_form.getValue('project_key');


  alert("value"+projectKey);


  var res=alpha.test(projectKey);


  if (!res) {


            g_form.showFieldMsg('project_key', 'Invalid input', 'error');


  }


}


Abhinay Erra
Giga Sage

snow123@



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


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


  return;


  }


  g_form.hideFieldMsg('sap_customer_number');


  if(/^[0-9a-zA-Z]+$/.test(newValue) && newValue.length==10){


  g_form.hideFieldMsg('sap_customer_number');


  }


  else{


  g_form.showFieldMsg('sap_customer_number','Invalid input','error');


  }


}


drjohnchun
Tera Guru

Can you try



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



  g_form.hideFieldMsg('sap_customer_number');
  if (!/^[a-zA-Z0-9]{10}$/.test(newValue))
      g_form.showFieldMsg('sap_customer_number','Invalid input','error');
}



Hope this helps.



Please feel free to connect, follow, mark helpful / answer, like, endorse.


John Chun, PhD PMP see John's LinkedIn profile

visit snowaid


ServiceNow Advocate

Winner of November 2016 Members' Choice Award


2017 ServiceNow Community Leader (January)


You can simplify your test logic by checking for the length in RegEx using "{10}", which says "match exactly 10 times."