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

Bharath40
Giga Guru

Hi,



Try this




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


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


  return;  


  }  


  g_form.hideFieldMsg('yourfField');  


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


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


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


 


}  


Hello Bharath,



Tried this didn't work. It is no throwing error of the field length increases or decreases 10.



Thanks,


SD


fredflintstone
Kilo Expert

It looks like your second or condition is wrong.   Where you have



patt.length !="10"



should be



newValue.length != "10"


Hi Fredflintstone,



Tried this didn't work.



Thanks,


SD