Field validation using Client script

Meenal Gharat
Giga Guru

 Hi ,

Can someone help me to write Client script to validate UserID variable on Catalog item.

User should be able to enter firstname.lastname in UserID field.

If user enters firstname'  ' lastname, it should show alert message.

 

Regards,

Meenal Gharat

1 ACCEPTED SOLUTION

Harsh Vardhan
Giga Patron

there would be multiple test scenario will be added for this field design,is it possible if you can create two variable on form, First name and Last NAme and make user id variable as read only.

what will happen if you will add any char in First name and any char in Last name then the combination of these two variables will be added to user id field. 

example: First Name contain Harsh and Last name Contain Vardhan so user id variable would be harsh.vardhan

 

now you can put the validation the first name variable and last name variable to not allow user to enter any special character or space in it. this way you can also design your form. 

 

 

below code you can use on First Name variable and Last name variable. 

 

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


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


   return;


   }


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


   if(!regex.test(newValue)){


   alert('Incorrect Value');


   g_form.setValue('u_first_name','');



   }


}

 

once all the validation will be ok then you can add the script to add the first name +'.'+ last name

 

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


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


   return;


   }


   var fn = g_form.getValue('u_first_name');
var ln = g_form.getValue('u_last_name');
var res = fn+'.'+ln;
g_form.setValue('u_user_id',res);


}

View solution in original post

3 REPLIES 3

Varsha21
Giga Guru

Hi,

 

get the value of first name and last name using

 var fname=g_form.getValue('variable name');

var lname=g_form.getValue('variable name');

alert(g_form.setValue(userid,fname + "." + lname));

 

better you can share screenshot,variables and expected o/p,oncahne or onsubmit.

 

 

 

Harsh Vardhan
Giga Patron

there would be multiple test scenario will be added for this field design,is it possible if you can create two variable on form, First name and Last NAme and make user id variable as read only.

what will happen if you will add any char in First name and any char in Last name then the combination of these two variables will be added to user id field. 

example: First Name contain Harsh and Last name Contain Vardhan so user id variable would be harsh.vardhan

 

now you can put the validation the first name variable and last name variable to not allow user to enter any special character or space in it. this way you can also design your form. 

 

 

below code you can use on First Name variable and Last name variable. 

 

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


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


   return;


   }


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


   if(!regex.test(newValue)){


   alert('Incorrect Value');


   g_form.setValue('u_first_name','');



   }


}

 

once all the validation will be ok then you can add the script to add the first name +'.'+ last name

 

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


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


   return;


   }


   var fn = g_form.getValue('u_first_name');
var ln = g_form.getValue('u_last_name');
var res = fn+'.'+ln;
g_form.setValue('u_user_id',res);


}

Harsh Vardhan
Giga Patron

if your query has resolved, kindly mark the answer correct and close this thread.